如何使用具有新值的java在mongodb中的现有集合中追加现有数组

时间:2022-05-31 21:22:44

I have a mongo collection like:

我有一个mongo集合,如:

{
    "_id": ObjectId("55cad746aed75601b4822cc9"),
    "entityId": "12",
    "entityType": "a",
    "nameIdentity": [{
        "fName": "abc",
        "lName": "def",
        "dob": "00",
        "address": "xyz"
    },

    ]
}

I am using mongodb java 3.0 driver and trying to match and update. For eg: I am trying to match on entityId if it found then add the new nameIdentity.

我正在使用mongodb java 3.0驱动程序并尝试匹配和更新。例如:我试图匹配entityId,如果它找到,然后添加新的nameIdentity。

Second time when I pass

我第二次通过

{
    "fName": "123",
    "lName": "456",
    "dob": "00",
    "address": "789"
}

For my entityId: 12 if it matches then my new collection should like this:

对于我的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"
    }]
}

I want to add it in the same matched object or collection. But its replacing the previous array and adding new like this:

我想将它添加到相同的匹配对象或集合中。但它取代了以前的数组,并添加了这样的新:

{
    "_id": ObjectId("55cad746aed75601b4822cc9"),
    "entityId": "12",
    "entityType": "a",
    "nameIdentity": [

    {
        "fName": "123",
        "lName": "456",
        "dob": "00",
        "address": "789"
    }
    ]
}

When entity id is matched I want everything to be added not updated. The code I tried is :

当实体ID匹配时,我想要添加所有内容而不更新。我尝试的代码是:

mongoDatabase.getCollection("entity").findOneAndUpdate(
    updateDocument, new Document("$set",entityDocument));

I tried with $push and $set. Its creating a new nameIdentity Array. But I want to add in same matched nameIdentity array. Any suggestions where am I going wrong ?

我尝试使用$ push和$ set。它创建了一个新的nameIdentity数组。但我想添加相同匹配的nameIdentity数组。有什么建议我哪里出错了?

2 个解决方案

#1


5  

You should use $push like following:

你应该使用$ push如下:

db.collection.update({
    "entityId": "12"
}, {
    $push: {
    "nameIdentity": {
        "fName": "123",
        "lName": "456",
        "dob": "00",
        "address": "789"
    }
    }
})

Its equivalent query using mongo java driver is something like (tested) :

使用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"))));

If you want to update many documents then use updateMany instead of updateOne by passing required params.

如果要更新许多文档,请通过传递必需的参数来使用updateMany而不是updateOne。

#2


1  

You basically want to $push and add to the named array entry here. But for .findOneAndUpdate() you also need to set the ReturnDocument type in order to receive the result.

你基本上想要$ push并在这里添加到命名数组条目。但是对于.findOneAndUpdate(),您还需要设置ReturnDocument类型以便接收结果。

Otherwise the "original" document is returned, just as it is for all drivers.

否则返回“原始”文档,就像所有驱动程序一样。

    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());

#1


5  

You should use $push like following:

你应该使用$ push如下:

db.collection.update({
    "entityId": "12"
}, {
    $push: {
    "nameIdentity": {
        "fName": "123",
        "lName": "456",
        "dob": "00",
        "address": "789"
    }
    }
})

Its equivalent query using mongo java driver is something like (tested) :

使用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"))));

If you want to update many documents then use updateMany instead of updateOne by passing required params.

如果要更新许多文档,请通过传递必需的参数来使用updateMany而不是updateOne。

#2


1  

You basically want to $push and add to the named array entry here. But for .findOneAndUpdate() you also need to set the ReturnDocument type in order to receive the result.

你基本上想要$ push并在这里添加到命名数组条目。但是对于.findOneAndUpdate(),您还需要设置ReturnDocument类型以便接收结果。

Otherwise the "original" document is returned, just as it is for all drivers.

否则返回“原始”文档,就像所有驱动程序一样。

    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());