如何使用带有新值的java在mongodb的现有集合中追加现有数组
我有一个mongo系列,比如:如何使用带有新值的java在mongodb的现有集合中追加现有数组,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,我有一个mongo系列,比如: { "_id": ObjectId("55cad746aed75601b4822cc9"), "entityId": "12", "entityType": "a", "nameIdentity": [{ "fName": "abc", "lName": "def", "dob": "00", "address": "xyz" }, ] } 我正在使
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [{
"fName": "abc",
"lName": "def",
"dob": "00",
"address": "xyz"
},
]
}
我正在使用mongodbjava3.0
驱动程序,并试图进行匹配和更新。例如:我正在尝试匹配entityId
,如果找到了,则添加新的nameIdentity
第二次当我经过的时候
{
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
对于我的entityId:12
,如果它匹配,那么我的新收藏应该如下所示:
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [{
"fName": "abc",
"lName": "def",
"dob": "00",
"address": "xyz"
}, {
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}]
}
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [
{
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
]
}
我想将其添加到相同的匹配对象或集合中。但它将替换以前的阵列并添加新阵列,如下所示:
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [{
"fName": "abc",
"lName": "def",
"dob": "00",
"address": "xyz"
}, {
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}]
}
{
"_id": ObjectId("55cad746aed75601b4822cc9"),
"entityId": "12",
"entityType": "a",
"nameIdentity": [
{
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
]
}
当实体id匹配时,我希望所有内容都被添加而不是更新。我尝试的代码是:
mongoDatabase.getCollection("entity").findOneAndUpdate(
updateDocument, new Document("$set",entityDocument));
我尝试了$push
和$set
。它正在创建一个新的nameIdentity
数组。但我想添加相同的匹配nameIdentity
数组。有什么建议我哪里出了问题吗?您应该像下面这样使用:
db.collection.update({
"entityId": "12"
}, {
$push: {
"nameIdentity": {
"fName": "123",
"lName": "456",
"dob": "00",
"address": "789"
}
}
})
使用mongo java驱动程序的等价查询类似于(已测试):
db.getCollection("entity").updateOne(new Document("entityId", "12"),
new Document("$push", new Document("nameIdentity", new Document("fName", "123").append("lName", "456")
.append("dob", "00").append("address", "789"))));
如果要更新多个文档,请通过传递所需的参数来使用而不是updateOne
。您基本上希望在此处添加到命名数组项。但是,为了接收结果,您还需要设置类型
否则将返回“原始”文档,就像所有驱动程序一样
Document entityDocument = new Document();
entityDocument.append("fname","123");
entityDocument.append("lname","456");
entityDocument.append("dob","00");
entityDocument.append("address","789")
Document doc = mongoDatabase.getCollection("entity").findOneAndUpdate(
new Document("entityId", 12),
new Document("$push", new Document("nameIdentity", entityDocument)),
new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
);
System.out.println(doc.toJson());
如果我正在传递多个数组,例如名称{}、地址{}、资格{},是否有任何快捷方式进行更新,或者我必须在更新查询中手动添加所有这些数组?@ShaikMujahidAli你是什么意思?是否要使用
entityId:12
在nameIdentity
中推送多个对象?如果是,则创建此类值的列表,并将其传递给$push
。在匹配实体id 12后,我需要检查fName、lName、address、dob是否存在,如果存在,则不执行上述添加操作。你现在可以帮我处理一下吗?你应该使用$exists
检查其他字段是否存在,就像我在这里检查的名称一样-db.getCollection(“entity”).updateOne(新文档(“entityId”,“12”).append(“nameIdentity.dob”,新文档($exists),false)),新文档($push),新文档(“nameIdentity”,新文档(“fName”,“1223”).append(“lName”,“2222222”).append(“dob”,“00”).append(“address”,“789”));
如果我传递多个数组,例如name{},address{},qualification{},是否有快捷方式进行更新,或者我必须在更新查询中手动添加所有这些数组?@ShaikMujahidAli实际上我已经向您展示了“正确的“语法,您选择不接受。如果您有新问题,请发布其他问题。