首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb $nin on ObjectId不工作

mongodb $nin on ObjectId不工作
EN

Stack Overflow用户
提问于 2016-04-04 19:04:00
回答 1查看 2.9K关注 0票数 2

我正在尝试运行更新子查询样式,在MongoDB上使用not in clase,但它不起作用。我认为这是因为toArrayObjectIds转换为文本,而不是将其保留为DBRef。下面是我的代码:

代码语言:javascript
复制
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”是DBRef集合中的listProducts字段。“产品”是我想要更新的系列。

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

我做错了什么?谢谢。

listsProducts:

{ "_id“:ObjectId(54e4bf7bade0276f008b4567), "__type“:”核心\Libs\列表\实体\产品“, “类型”:"inventoryItem“ “产品”:DBRef(“产品”,ObjectId(“54e308e23b8e778d128b4799”) “列表”:DBRef(“列表”,ObjectId(“54e4aeab5252416c008b4569”) "inventoryData“:{ “"Core\Libs\Listing\Entity\Product\InventoryData",”:__type "parLevel“:NumberLong(0), "itemsOnHand“:NumberLong(0) }, "timeLog“:{ "__type“:”核心\Utils\实体\时间日志“, "createdAt“:ISODate("2015-02-18T16:36:11.387+0000"), "updatedAt“:ISODate(”2015-07T07:31:25.900+0000“), "deletedAt“:空 } }

产品:

{ "_id“:ObjectId(54e308d83b8e778d128b4588), "__type“:”核心\Libs\产品\实体\产品“, “名字”:“胡萝卜片” "gtin“:"10071179184300” “地位”:“积极”, "defaultPrice“:NumberLong(0), “参考资料”:{ “"Core\Libs\Product\Entity\Product\References",”:__type “制造商”:DBRef(“制造商”,ObjectId(“54e308d73b8e778d128b4569”) “类别”:DBRef(“1WS-类别”,ObjectId("53e1e8723b8e77a52b8b45fd")) }, “信息”:{ “"Core\Libs\Product\Entity\Product\Information",”:__type “描述”:{ “"Core\Libs\Product\Entity\Product\Description",”:__type “短”:“胡萝卜1/20#", “长”:“简单经典-胡萝卜光滑Sli 1/20#” }, “属性”:{ "__type“:”Xeeo\Services\Core\文摘\Collection“, “实体”: { "__type“:”核心\Utils\实体\KeyValuePair“, “钥匙”:“品牌名称”, “价值”:“单纯经典” }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“GLN制造商”, 价值:"0071179000009“ }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“制造商名称”, “价值”:“J.R. Simplot公司” }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“原籍国”, “价值”:“美国” }, { "__type“:”核心\Utils\实体\KeyValuePair“, “键”:“最后修改日期”, “价值”:"2014-12-03T09:42:04“ }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“出版日期”, “价值”:"2011-10-26T00:00:00“ }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“开始提供日期”, “价值”:“2014-12-03 T00:00:00” }, { "__type“:”核心\Utils\实体\KeyValuePair“, “关键”:“深度”, 价值:"13.375“ } } }, “图像”:"http://www.fsenetportal.com/FSENetimages.nsf/0/BB29958620D9515A87257AA6005068B1/$file/10071179184300_A1CD.jpg?OpenElement", "elasticSearchIndexStatus“:”索引“, "timeLog“:{ "__type“:”核心\Utils\实体\时间日志“, "createdAt“:ISODate("2015-02-17T09:24:40.138+0000"), "updatedAt“:ISODate("2016-03-25T00:56:21.219+0000"), "deletedAt“:空 } }

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-04 19:29:15

{_id: {$nin: items}}需要一个ObjectIds数组,据我所见,您正在传递一个文档数组,更糟糕的是,您似乎在告诉mongo不要在find查询中选择_id

我就是这样做的。

代码语言:javascript
复制
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});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36410822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档