Mongoose/Mongo find in array of objectIds

梦想的初衷 提交于 2019-12-08 16:14:00

问题


I have this item in 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'.

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.

Similar questions on SO did not yield an answer.

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

Update:

I can fetch just fine from CLI using:

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

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

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


回答1:


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

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




回答2:


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.

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


来源:https://stackoverflow.com/questions/12451358/mongoose-mongo-find-in-array-of-objectids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!