如何使用java在mongodb的嵌套数组中添加元素
大家好,我正在使用java开发mongodb,我有一个场景,如果外部文档匹配,那么我必须更新/添加嵌套数组中的计数,例如,我想做如下操作:如何使用java在mongodb的嵌套数组中添加元素,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,大家好,我正在使用java开发mongodb,我有一个场景,如果外部文档匹配,那么我必须更新/添加嵌套数组中的计数,例如,我想做如下操作: `"_id" : ObjectId("55d71603aed7562284e5df95"), "id" : "1", "type" : "a", "score" : { "mark1" : "1", "mark2" : "2", "count" : { "one","two"
`"_id" : ObjectId("55d71603aed7562284e5df95"),
"id" : "1",
"type" : "a",
"score" : {
"mark1" : "1",
"mark2" : "2",
"count" : { "one","two"
}
}`
如果我再次发送一个具有相同字段的文档,如id:1、type:a、mark1:1、mark2:2,那么我必须以
`"_id" : ObjectId("55d71603aed7562284e5df95"),
"id" : "1",
"type" : "a",
"score" : {
"mark1" : "1",
"mark2" : "2",
"count" : { "one","two","three"
}
}`
但我得到的是这样的东西:
`"_id" : ObjectId("55d71e42aed7560e8c9d02e4"),
"id" : "1",
"type" : "a",
"score" : {
"mark1" : "1",
"mark2" : "2",
"count" : {
"count" : "one",
}
}`
我的java代码是
`mongoDatabase=mongoClient.getDatabase("TestNestedInsert");
Document sourceDocument=mongoDatabase.getCollection("entity").find(new Document("id",1).append("score.mark1", "1").append("score.mark2", "2")).first();
if(sourceDocument==null){
Document entity=new Document();
entity.append("id", "1");
entity.append("type", "a");
entity.append("score", new Document("mark1","1").append("mark2", "2").append("count", new Document("count","one")));
mongoDatabase.getCollection("entity").insertOne(entity);
}
else{
mongoDatabase.getCollection("entity").findOneAndUpdate(new Document("id",1).append("score.mark1", "1").append("score.mark2", "2"), new Document("$set",new Document("score.count","three")));
}
`
我知道我们不能有重复的钥匙,我试过$set和$push,但我卡住了。有什么帮助吗?主要问题是,根据代码,计数键是一个对象而不是数组。因此,正确的JSON表示法是:
{
"_id": ObjectId("55d71603aed7562284e5df95"),
"id": "1",
"type": "a",
"score": {
"mark1": "1",
"mark2": "2",
"count": [
"one",
"two",
"three"
]
}
}
请注意计数键后的方括号[]
关于新的mongo java驱动程序3.0,我也有同样的问题。
我是这样解决的:
MongoDatabase mongoDatabase = mongoClient.getDatabase("TestNestedInsert");
MongoCollection<Document> entityCollection = mongoDatabase.getCollection("entity");
Document queryDocument = new Document("id", 1).append("score.mark1", "1").append("score.mark2", "2");
Document sourceDocument = entityCollection.find(queryDocument).first();
if (sourceDocument == null) {
Document entity = new Document();
entity.append("id", "1");
entity.append("type", "a");
// Create the score nested object
String[] countArray = { "one", "two" }; // create the count array
Document scoreObject = new Document()
.append("mark1", "1")
.append("mark2", "2")
.append("count", countArray); // append the count array
entity.append("score", scoreObject); // append the score object.
entityCollection.insertOne(entity);
}
else{
// push the new element to the count array.
// see: https://docs.mongodb.org/v3.0/reference/operator/update/push/#append-a-value-to-an-array
Document elementToArray = new Document("score.count", "three");
Document pushElement = new Document("$push", elementToArray);
entityCollection.updateOne(queryDocument, pushElement);
}
希望有帮助 问题是您正在使用$set设置字段。您应该使用$push操作符推送。$set运算符用指定的值替换字段的值。$push运算符将指定的值附加到数组中。不,我也无法使用$push,我尝试使用$set和$push。这与否无关,我无法使用。你是。但是,您应该使用数组。计数字段不是数组。这就是“推”不起作用的原因。