1

I have a big problem with the api java mongodb. I use a request with the update methods of DBCollection class and in the mongodb i get a multiple same document while the value doesn't change,help me please. I don't want to have a duplicate document.

BasicDBObject query = new BasicDBObject();
query.append("ad", "man2ist").append("list.id", new BasicDBObject("$ne", "5")); //  "list.id" : {$ne : 0 }

BasicDBObject a = new BasicDBObject("id",String.valueOf(5)).append("value", 100);
BasicDBObject upd = new BasicDBObject("$addToSet",new BasicDBObject("list",a));
System.out.println(query);

System.out.println(upd);

WriteResult r = dbc.update(query,upd,true,false);

//db.friends.update({ "ad" : "man2ist" , "list.id" : { $ne : "4"} },{ $addToSet : { "list" : { "id" : "4","value" : 100}}},true,true);

my document here :

{
"ad" : "man2ist",
"createdDate" : ISODate(),
"list" : [
        {
                "id" : "45",
                "value" : 489
        },
        {
                "id" : "5",
                "value" : 20,

        },
        {
               "id" : "4578",

                "value" : 21,

        } ]}
4

1 回答 1

0

问题是您将不满意标志设置为 true,这意味着如果没有找到符合条件的文档,则应该创建该文档。如果数据库中没有符合条件的文档,mongo shell 也会这样做。

如果您将查询更改为此,

WriteResult r = dbc.update(query,upd,false,false);

它应该一直有效。

于 2013-07-24T20:33:30.400 回答