[英]How to update Mongo DB document with complex structure
I have a mongoDB document with the following structure 我有一个具有以下结构的mongoDB文档
{
"id": 9595511812,
"Feeds": [
{
"department": "mseb",
"consumer_number": 1234567890,
"due_date": "2016-11-11",
"due_amount": 400,
"balance_amount": 0,
"unitsConsumed": 40,
"freezeDate": "2016-11-11",
"lastPaidDate": "2016-11-11",
"billNumber": "9877",
"id": "1",
"paid": false
},
{
"department": "mseb",
"consumer_number": 1234567890,
"due_date": "2016-11-21",
"due_amount": 400,
"balance_amount": 0,
"unitsConsumed": 40,
"freezeDate": "2016-11-21",
"lastPaidDate": "2016-11-21",
"billNumber": "9877",
**"id": "2",**
"paid": false
}
]
}
I want to update paid == true for Feed with id=2 (** marked field above). 我想为id = 2(上面带有**标记的字段)的Feed更新pay == true。 How can I do that in using mongo java client?
如何在使用mongo java客户端时做到这一点?
I have tried 我努力了
DB db = DBConnection.getDatabaseConnection();
DBCollection table = db.getCollection("customer");
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("Feeds.paid", "true"));
BasicDBObject searchQuery = new BasicDBObject().append("id", "9595511812");
table.update(searchQuery, newDocument);
Feeds
is an array, and you're updating the 2nd element, so the set
would end with: Feeds
是一个数组,您要更新第二个元素,因此该set
将以:
.append("Feeds.1.paid", "true"));
instead of: 代替:
.append("Feeds.paid", "true"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.