简体   繁体   English

Mongodb:无法保存添加到BasicDBList中的新元素

[英]Mongodb: Can't save a new element added into BasicDBList

I am trying to add an element to a BasicDBList array and save it back to Mongodb, but when I check the result, it is not written. 我试图将一个元素添加到BasicDBList数组中并将其保存回Mongodb,但是当我检查结果时,它没有被写入。 What did I do wrong? 我做错了什么? I use java driver version 2.7.2. 我使用Java驱动程序版本2.7.2。

DBObject dbObject = coll.findOne(dbQuery);

BasicDBList unreadMsgs = (BasicDBList) dbObject.get("unreadMsgs");

Logger.debug("before incrementing unreadMsgs" + dbObject.toString());

unreadMsgs.add(new BasicDBObject("id", 1).append("unreadMsg", 1));
Logger.debug("after incrementing unreadMsgs : " + dbObject.toString());
coll.save(dbObject);
Logger.debug("check result: " + coll.findOne(dbQuery).toString());

before incrementing unreadMsgs{ "_id" : { "$oid" : "515c5eb88e3278e9c9d55867"} , "unreadMsgs" : [ ]} 在递增unreadMsgs {“之前:”“ oidoid”:“ 515c5eb88e3278e9c9d55867”},“ unreadMsgs”:[]}

after incrementing unreadMsgs : { "_id" : { "$oid" : "515c5eb88e3278e9c9d55867"} , "unreadMsgs" : [ { "id" : 1 , "unreadMsg" : 1}]} 递增unreadMsgs后:{“ _id”:{“ $ oid”:“ 515c5eb88e3278e9c9d55867”},“ unreadMsgs”:[{“ id”:1,“ unreadMsg”:1}]}

check result: { "_id" : { "$oid" : "515c5eb88e3278e9c9d55867"} , "unreadMsgs" : [ ]} 检查结果:{“ _id”:{“ $ oid”:“ 515c5eb88e3278e9c9d55867”},“ unreadMsgs”:[]}

The problem is that the coll.save(dbObject) is not updating anything. 问题是coll.save(dbObject)没有更新任何内容。

It works as an insert and, since the _id already exists in the collection, you are getting a duplicateKey exception (you are just not seeing it because of configuration). 它用作插入,并且由于_id已存在于集合中,因此您将得到一个plicateKey异常(由于配置而没有看到它)。

You have to use an update, here is how 你必须使用一个更新, 这里是怎么

The save call should work on that case, but I suggest you use an update with $addToSet operation. 在这种情况下, save调用应该可以工作,但是我建议您使用$ addToSet操作进行更新

Here's the code: 这是代码:

DBObject addToSetObj = BasicDBObjectBuilder.start()
    .push("$addToSet")
        .push("unreadMsgs")
            .add("id", 1)
            .add("unreadMsg", 1)
        .pop()
    .pop()
.get();

// addToSetObj will be { "$addToSet" : { "unreadMsgs" : { "id" : 1 , "unreadMsg" : 1}}}


coll.update(dbQuery, addToSetObj);
Logger.debug("check result: " + coll.findOne(dbQuery).toString());

Any doubts on how to use addToSet, check this out: http://docs.mongodb.org/manual/reference/operator/addToSet/ 有关如何使用addToSet的任何疑问,请查看以下网址http ://docs.mongodb.org/manual/reference/operator/addToSet/

Thanks for everybody's answer. 谢谢大家的回答。 I found the real problem. 我发现了真正的问题。 It turns out that my collection is capped, and I am not allowed to insert more data into an existing document in a capped collection. 事实证明我的收藏集是有上限的,并且不允许我将更多数据插入有上限的收藏集中的现有文档中。 I saw the exception after I changed WriteConcern to FSYNC_SAFE. 将WriteConcern更改为FSYNC_SAFE后,我看到了异常。 I changed all my collections to uncapped, and the code works now. 我将所有集合更改为无上限,并且代码现在可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM