【问题标题】:mongodb $nin on ObjectId not workingObjectId 上的 mongodb $nin 不起作用
【发布时间】:2016-07-24 11:26:40
【问题描述】:

我正在尝试在 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});

'product' 是 listProducts 集合中的 DBRef 字段。 'products' 是我要更新的集合。

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

我做错了什么?谢谢。

列表产品:

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

产品:

{
"_id" : ObjectId("54e308d83b8e778d128b4588"),
“__type”:“核心\库\产品\实体\产品”,
"名称" : "胡萝卜片",
“gtin”:“10071179184300”,
“状态”:“活动”,
"defaultPrice" : NumberLong(0),
“参考”:{
"__type" : "Core\Libs\Product\Entity\Product\References",
"制造商" : DBRef("制造商", ObjectId("54e308d73b8e778d128b4569")),
"类别" : DBRef("1ws-categories", ObjectId("53e1e8723b8e77a52b8b45fd"))
},
“信息”:{
"__type" : "核心\库\产品\实体\产品\信息",
“描述”:{
"__type" : "Core\Libs\Product\Entity\Product\Description",
"short" : "Carrot Smooth Sli 1/20#",
"long" : "Simplot Classic - Carrot Smooth Sli 1/20#"
},
“属性”:{
"__type" : "Xeeo\Services\Core\Abstracts\Collection",
“实体”:[
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "品牌名称",
“价值”:“Simplot 经典”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "制造商 GLN",
“价值”:“0071179000009”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "制造商名称",
“价值”:“J. R. Simplot 公司”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "原产国",
“价值”:“美国”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "最后修改日期",
“价值”:“2014-12-03T09:42:04”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "出版日期",
“价值”:“2011-10-26T00:00:00”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "开始可用日期",
“价值”:“2014-12-03T00:00:00”
},
{
"__type" : "Core\Utils\Entities\KeyValuePair",
"key" : "深度 (IN)",
“价值”:“13.375”
}
]
}
},
"图片" : "http://www.fsenetportal.com/FSENetimages.nsf/0/BB29958620D9515A87257AA6005068B1/$file/10071179184300_A1CD.jpg?OpenElement",
"elasticSearchIndexStatus" : "索引",
“时间日志”:{
"__type" : "Core\Utils\Entity\TimeLog",
"createdAt" : ISODate("2015-02-17T09:24:40.138+0000"),
"updatedAt" : ISODate("2016-03-25T00:56:21.219+0000"),
“deletedAt”:空
}
}

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    {_id: {$nin: items}} 需要一个 ObjectIds 数组,据我所知,您正在传递一组文档,更糟糕的是,您似乎告诉 mongo 不要在您的 findquery 中选择 _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});
    

    【讨论】:

    • "product" 是一个 DBRef,所以我需要 map 函数返回的是 i.product._id。但是,当我更新您的地图函数以返回 i.product._id 时,我得到以下信息: WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 2 , "errmsg" : "ArrayFilterEntries 相等不能是未定义的" } })
    • 您能否发布来自itemsproductscollections 的文档示例?
    • 我想通了。应该是这样的:return i.product.$id;
    • {_id: {$exists: true}} 是多余的,因为它总是存在于每个集合中的每个对象中
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多