[英]Index is not getting created, text index required for $text query - mongoose
[英]Getting element in array that matched text index query
我在猫鼬上设置了以下架构
{
_id:
store:
offers:[{
name:
price:
}]
}
我决定索引offer.name如下
uniSchema.index({'offers.name':'text'});
现在我正在尝试对该索引进行搜索
Stores.find({$text:{$search:VALUE}}, callback);
但是这样一来,只要搜索成功,就会列出整个商店,而我无法弄清楚匹配来自哪个报价。
有没有办法用猫鼬的索引做到这一点? 找出与查询匹配的数组元素?
我不确定$text
索引是否可行。
对于直接查询,您可以使用投影来执行以下操作:
> db.array_find.find({ "offers.name": "firstname"},{"offers.$" : 1} )
但是文本查询不会直接引用数组,因此不能在投影中使用offers.name
。
> db.array_find.find({ $text: { $search: "firstname"} },{"offers.$" : 1} )
error: {
"$err" : "Can't canonicalize query: BadValue Positional projection 'offer.$' does not match the query document.",
"code" : 17287
}
尝试对结果文档中的数组进行任何类型的后处理的问题是,您将不会使用mongo的文本索引,而是将其近似使用。
您可能需要其他数据结构
_id:
store: 2
offer:
name: this is a single offer
price: 4
_id:
store: 2
offer:
name: this is the next offer
price: 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.