首页 文章

关于ObjectId的mongodb $ nin无法正常工作

提问于
浏览
0

我试图在MongoDB上运行一个更新子查询样式,但是它不起作用 . 我相信它是因为 toArrayObjectIds 强制转换为文本而不是将其作为 DBRef . 以下是我的代码:

var items = db.listsProducts.find({_id: {$exists: true}}, {product: true, _id: false}).toArray();
db.products.update({_id: {$nin: items}}, {$set: {'status':'inactive'}},{multi:true});

'product'是listProducts集合中的DBRef字段 . 'products'是我想要更新的系列 .

它不会更新不在“项目”中的字段,而是更新所有文档 .

我究竟做错了什么?谢谢 .

listsProducts:

{“_ id”:ObjectId(“54e4bf7bade0276f008b4567”),“__ type”:“Core \ Libs \ Listing \ Entity \ Product”,“type”:“inventoryItem”,“product”:DBRef(“products”,ObjectId(“ 54e308e23b8e778d128b4799“)),”list“:DBRef(”lists“,ObjectId(”54e4aeab5252416c008b4569“)),”inventoryData“:{”_ _ type“:”Core \ Libs \ Listing \ Entity \ Product \ InventoryData“,”parLevel“: NumberLong(0),“itemsOnHand”:NumberLong(0)},“timeLog”:{“_ _ type”:“Core \ Utils \ Entity \ TimeLog”,“createdAt”:ISODate(“2015-02-18T16:36:11.387” 0000“),”updatedAt“:ISODate(”2015-07-07T07:31:25.900 0000“),”deletedAt“:null}}

产品:

{“_ id”:ObjectId(“54e308d83b8e778d128b4588”),“__ type”:“Core \ Libs \ Product \ Entity \ Product”,“name”:“Carrot Slices”,“gtin”:“10071179184300”,“status”: “active”,“defaultPrice”:NumberLong(0),“references”:{“_ _ type”:“Core \ Libs \ Product \ Entity \ Product \ References”,“manufacturer”:DBRef(“manufacturer”,ObjectId(“54e308d73b8e778d128b4569” “)),”category“:DBRef(”1ws-categories“,ObjectId(”53e1e8723b8e77a52b8b45fd“))},”information“:{”_ _ type“:”Core \ Libs \ Product \ Entity \ Product \ Information“,”description “:{”_ _ type“:”Core \ Libs \ Product \ Entity \ Product \ Description“,”short“:”Carrot Smooth Sli 1/20#“,”long“:”Simplot Classic - Carrot Smooth Sli 1/20# “},”attributes“:{”_ _ type“:”Xeeo \ Services \ Core \ Abstracts \ Collection“,”entities“:[{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”Brand名称“,”值“:”Simplot Classic“},{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”制造商GLN“,”值“:”0071179000009“},{”_ _ type“ :“Core \ Utils \ Entities \ KeyValuePair“,”key“:”制造商名称“,”值“:”J . R. Simplot Company“},{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”Country of Origin“,”value“:”US“},{”__ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”上次修改日期“,”值“:”2014-12-03T09:42:04“},{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”键“:”发布日期“,”值“:”2011-10-26T00:00:00“},{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”Start Availability Date“,” value“:”2014-12-03T00:00:00“},{”_ _ type“:”Core \ Utils \ Entities \ KeyValuePair“,”key“:”Depth(IN)“,”value“:”13.375“} ]},“images”:“http://www.fsenetportal.com/FSENetimages.nsf/0/BB29958620D9515A87257AA6005068B1/$file/10071179184300_A1CD.jpg?OpenElement”,“elasticSearchIndexStatus”:“indexed”,“timeLog”:{ “__type”:“Core \ Utils \ Entity \ TimeLog”,“createdAt”:ISODate(“2015-02-17T09:24:40.138 0000”),“updatedAt”:ISODate(“2016-03-25T00:56:21.219” 0000“),”deletedAt“:null}}

1 回答

  • 0

    {_id: {$nin: items}} 期待一个 ObjectIds 数组,从我可以看到你更糟糕的是你似乎在告诉mongo不要在你的 find 查询中选择 _id .

    我就是这样做的 .

    var items = db.listsProducts.find({_id: {$exists: true}}, {product: true, _id: true}).toArray();
    
    var itemIds = items.map(function(i) {
        return i._id;
    });
    
    db.products.update({_id: {$nin: itemIds}}, {$set: {status: 'inactive'}}, {multi: true});
    

相关问题