插入对象MongoDB的数组

时间:2017-12-24 22:19:43

标签: node.js mongodb mongoose mongodb-query

我有

{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "abc@gmail.com",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [ 
        {
            "name" : "We",
            "arr" : [ 
                "5a26d554677475818a795f75", 
                "5a395bb0976d5304c07f7dd4"
            ]
        }, 
        {
            "name" : "sandeep",
            "arr" : [ 
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

我想在list.arr中添加一个元素,其中name ='我们' 并且仅在元素不存在时添加

我该如何执行此查询。

3 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,您希望将name字段与we键匹配,并仅在arr存在时更新db.test.update({ list: { $elemMatch: { name: "We" , arr: { $nin: [ "valuette" ] }} } }, { $push: { "list.$.arr": "valuette" } } ); ,您必须使用elemMatch,而不是获得正确的文件

$

“list。$。arr”中的name匹配将we字段与{{1}}值匹配的指定索引

更新:回答OP问题

答案 1 :(得分:1)

初始数据库结构

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "abc@gmail.com",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

执行查询

> db.collection.update({ list: { $elemMatch: { name: "We" , arr: {$nin: ["55555555555555555"]} } } }, {  $push: { "list.$.arr": "55555555555555555" } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

新数据库结构

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "abc@gmail.com",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4",
                "55555555555555555"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

再次执行相同的查询

> db.collection.update({ list: { $elemMatch: { name: "We" , arr: {$nin: ["55555555555555555"]} } } }, {  $push: { "list.$.arr": "55555555555555555" } } )
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

再次检查数据库结构

> db.collection.find().pretty()
{
    "_id" : ObjectId("5a25af80d2c4dd065991dccc"),
    "username" : "abc@gmail.com",
    "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy",
    "role" : "Admin",
    "__v" : 0,
    "list" : [
        {
            "name" : "We",
            "arr" : [
                "5a26d554677475818a795f75",
                "5a395bb0976d5304c07f7dd4",
                "55555555555555555"
            ]
        },
        {
            "name" : "sandeep",
            "arr" : [
                "5a26d554677475818a795f75"
            ]
        }
    ]
}

未找到任何更改

答案 2 :(得分:0)

通过使用这个嵌套查询,我解决了这个问题。 我正在使用Node JS

router.post('/prevList', (req, res) => {
  User.update({'_id':req.user.id, list: { $elemMatch: { name: req.body.name } } },{  $addToSet: { "list.$.arr": {$each: req.body.arr} } },function(err,data) {
    console.log(data);
    res.send(data)
  });
});

这里

req.body: {name:'we',arr:["5a395bb0976d5304c07f7dd4"]}

req.user.id是auth id。

希望这也可以帮助你。