Mongoose/Mongo在objectIds数组中查找 [英] Mongoose/Mongo find in array of objectIds
问题描述
我在mongo中有此商品:
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')})
但是没有任何效果.我可以使用_id:'5052843e023273693300013c'通过id很好地获取.
But nothing works. I can fetch by id just fine using: _id: '5052843e023273693300013c'.
请注意,插入产品时,类别ID是作为字符串添加的(这意味着我只是分配了ID而不是类别对象,但这并不能解释为什么上述任何一项都不起作用-它在转储中没有被引用,所以也许Mongo识别为对象ID.
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.
关于SO的类似问题没有给出答案.
Similar questions on SO did not yield an answer.
我正在使用最新的Mongoose(3种东西)和最新的Mongo,Node.
I am using the latest Mongoose (3 something) and recent Mongo,Node.
更新:
我可以使用以下命令从CLI中获取数据:
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' }})
我的架构如下:
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 }
});
谢谢
推荐答案
正在发生的事情是,猫鼬在Product.find
调用中将您正在使用的任何值转换为categories
值的对象ID,因为categories
值具有字符串类型而不是ObjectId,因此不匹配.
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.
这篇关于Mongoose/Mongo在objectIds数组中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!