mongodb Mongoose/Mongo 在 objectIds 数组中查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12451358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:50:09  来源:igfitidea点击:

Mongoose/Mongo find in array of objectIds

mongodbmongoosenodes

提问by cyberwombat

I have this item in mongo:

我在 mongo 中有这个项目:

[ { title: 'Product Name',
_id: 5052843e023273693300013c,
description: 'This is a fake description',
categories: [ 5052843e023273693300010a ],
} ]

I want to find products like this that have this category. I have tried:

我想找到此类具有此类别的产品。我试过了:

Product.find({ categories:  mongoose.Types.ObjectId('5052843e023273693300010a')})
Product.find({ categories:  mongoose.mongo.BSONPure.ObjectID.fromString('5052843e023273693300010a')})
Product.find({ categories:  '5052843e023273693300010a'})
Product.find({ 'categories':  '5052843e023273693300010a'})
Product.find({ categories:  {$in: ['5052843e023273693300010a']}})
Product.find({ categories:  Schema.Types.ObjectId('5052843e023273693300010a')})

But nothing works. I can fetch by id just fine using: _id: '5052843e023273693300013c'.

但没有任何作用。我可以通过 id 很好地使用:_id:'5052843e023273693300013c'。

Note that when the products were inserted the category ID were added as a string (meaning I just assigned the ID instead of the category objects but that doesn't explain why none of the above work - it's unquoted in the dump so perhaps Mongo recognizes as an object ID.

请注意,当产品被插入时,类别 ID 被添加为一个字符串(意味着我只是分配了 ID 而不是类别对象,但这并不能解释为什么上述都不起作用——它在转储中没有被引用,所以也许 Mongo 识别为一个对象 ID。

Similar questions on SO did not yield an answer.

关于 SO 的类似问题没有产生答案。

I am using the latest Mongoose (3 something) and recent Mongo,Node.

我正在使用最新的 Mongoose(3 件)和最近的 Mongo,Node。

Update:

更新:

I can fetch just fine from CLI using:

我可以使用以下命令从 CLI 中正常获取:

db.products.find({ categories: '5052843e02327369330000fe' }); 

and interestingly I can fetch it by doing the not equal in my code - huh?:

有趣的是,我可以通过在我的代码中执行 not equal 来获取它 - 嗯?:

Product.find({ categories: { $ne: '5052843e02327369330000fe' }})

My schema is as follows:

我的架构如下:

    var Product = new Schema({
        title: { type: String, required: true },
        slug: { type: String },
        summary: { type: String }, //browser title
        description: { type: String, required: false },
        excerpt: { type: String },    //for list and also for meta description
        publish: { type: Boolean },
        featured: { type: Boolean },
        unavailable: { type: Boolean },
        model: { type: String },
        google: { type: String },
        tags: { type: Array },
        categories:  [{ type: Schema.Types.ObjectId, ref: 'Category' }],
        manufacturer: { type: String },
        variations: { type: Array },
        prices: { type: Array },
        images: { type: Array },
        specs: { type: Array },
        modified: { type: Date, default: Date.now }

    });

    var Category = new Schema({
        title: { type: String, required: true },
        description: { type: String },
        parent: { type: Schema.Types.ObjectId, ref: 'Category' },
        images: { type: Array }
    });

Thanks

谢谢

采纳答案by JohnnyHK

What's happening is that Mongoose is casting whatever value you're using for a categoriesvalue in your Product.findcall to an ObjectId as that's how categoriesis defined in the schema. But the document's categoriesvalue you're trying to match has a string type instead of an ObjectId so it's not matching.

发生的事情是 Mongoose 正在转换您categoriesProduct.find调用 ObjectId 时使用的任何值,因为这categories就是架构中定义的方式。但是categories您尝试匹配的文档值具有字符串类型而不是 ObjectId,因此它不匹配。

To get things to work again you'll need to clean up your existing documents to match your defined schema.

为了让事情再次工作,您需要清理现有文档以匹配您定义的架构。

回答by Ash Blue

With Mongoose you have to force cast your string as an ID in some cases. Usually it automatically does this for you, but in your specific case it doesn't. The following snippet of code will get all connections with the passed ID.

在某些情况下,使用 Mongoose 必须强制将字符串转换为 ID。通常它会自动为您执行此操作,但在您的特定情况下它不会。以下代码片段将获取所有具有传递 ID 的连接。

var mongoose = require('mongoose');
Node.find({ connections: mongoose.Types.ObjectId("535c5c1b8aa6dc5f021e8a98") }, function (err, results) { 
    console.log(results); 
});