在符合条件的集合中查找并更新数组中的数据

时间:2018-10-28 13:44:07

标签: node.js mongodb mongoose mongodb-query mongoose-schema

我的数据库集合:

获奖者:

{
    "_id": {
        "$oid": "5bd58921fb6fc074abb12ce8"
    },
    "id": "301",
    "winner": [
        "101",
        "102"
    ]
}

这是收藏家赢家的榜样

var Schema = mongoose.Schema;

var winner = new Schema({
  _id: String,
  id: String,
  winner:[ String ]
},
  {
  strict: false
});
var Winner = mongoose.model('winner', winner);

var data = [ { _id: 5bd587adfb6fc074abb12cb2,
    name: 'name 1',
    year: '3',
    id: '101' },
  { _id: 5bd587cffb6fc074abb12cb8,
    name: 'name 2',
    year: '4',
    id: '102' } ];

    Winner.update("101":data, function(err, result){
console.log(result);
});

将数据变量中的值更新为与id字段匹配的获胜者集合数组(即,值101应该由具有id字段“ id”的值的数据变量中的json更新) :“ 101”

输出应为

优胜者收藏集应更新为

获奖者:

{
    "_id": {
        "$oid": "5bd58921fb6fc074abb12ce8"
    },
    "id": "301",
    "winner": [
        { _id: 5bd587adfb6fc074abb12cb2,
        name: 'name 1',
        year: '3',
        id: '101' },
        { _id: 5bd587cffb6fc074abb12cb8,
        name: 'name 2',
        year: '4',
        id: '102' }
    ]
}

先谢谢了:)

0 个答案:

没有答案