ObjectId上的mongodb $ nin无效

时间:2016-04-04 19:04:01

标签: mongodb

我正在尝试使用MongoDB上的not in clase运行更新子查询样式,但它不起作用。我相信它是因为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});

'产品'是listProducts集合中的DBRef字段。 '产品'是我试图更新的集合。

不是更新不在'项目中的字段,而是更新所有文档。

我做错了什么?感谢。

listsProducts:

  

{
     " _id" :ObjectId(" 54e4bf7bade0276f008b4567"),
     " __类型" :" Core \ Libs \ Listing \ Entity \ Product",
     "类型" :" inventoryItem",
     "产品" :DBRef(" products",ObjectId(" 54e308e23b8e778d128b4799")),
     "列表" :DBRef("列出",ObjectId(" 54e4aeab5252416c008b4569")),
     " inventoryData" :{
         " __类型" :" Core \ Libs \ Listing \ Entity \ Product \ InventoryData",
         " parLevel" :NumberLong(0),
         " itemsOnHand" :NumberLong(0)
     },
     " timeLog" :{
         " __类型" :" 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"),
     " __类型" :" Core \ Libs \ Product \ Entity \ Product",
     "名称" :"胡萝卜片",
     " GTIN" :" 10071179184300",
     "状态" :"活跃",
     " defaultPrice" :NumberLong(0),
     "参考文献" :{
         " __类型" :" Core \ Libs \ Product \ Entity \ Product \ References",
         "制造商" :DBRef("制造商",ObjectId(" 54e308d73b8e778d128b4569")),
         "类别" :DBRef(" 1ws-categories",ObjectId(" 53e1e8723b8e77a52b8b45fd"))
     },
     "信息" :{
         " __类型" :" Core \ Libs \ Product \ Entity \ Product \ Information",
         "描述" :{
             " __类型" :" Core \ Libs \ Product \ Entity \ Product \ Description",
             "短" :" Carrot Smooth Sli 1/20#",
             "长" :" Simplot Classic - Carrot Smooth Sli 1/20#"
         },
         "属性" :{
             " __类型" :" Xeeo \ Services \ Core \ Abstracts \ Collection",
             "实体" :[
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"品牌名称",
                     "值" :" Simplot Classic"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"制造商GLN",
                     "值" :" 0071179000009"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"制造商名称",
                     "值" :" J。 R. Simplot公司"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"原产国",
                     "值" :"美国"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"上次修改日期",
                     "值" :" 2014-12-03T09:42:04"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"出版日期",
                     "值" :" 2011-10-26T00:00:00"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"开始使用日期",
                     "值" :" 2014-12-03T00:00:00"
                 },
                 {
                     " __类型" :" Core \ Utils \ Entities \ KeyValuePair",
                     "键" :"深度(IN)",
                     "值" :" 13.375"
                 }
             ]
         }
     },
     "图像" :" http://www.fsenetportal.com/FSENetimages.nsf/0/BB29958620D9515A87257AA6005068B1/ $ file / 10071179184300_A1CD.jpg?OpenElement",
     " elasticSearchIndexStatus" :"索引",
     " timeLog" :{
         " __类型" :" 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 :(得分:0)

{_id: {$nin: items}}需要一个ObjectIds的数组,我可以看到你传递了一系列文档,而且你似乎更糟糕的是你似乎在告诉mongo不要选择_id查询中的find

我就是这样做的。

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