[英]Getting element in array that matched text index query
I have the following Schema set up on mongoose 我在猫鼬上设置了以下架构
{
_id:
store:
offers:[{
name:
price:
}]
}
I decided to indexed offers.name as follows 我决定索引offer.name如下
uniSchema.index({'offers.name':'text'});
and now I'm trying to do searches on that index 现在我正在尝试对该索引进行搜索
Stores.find({$text:{$search:VALUE}}, callback);
But this way whenever there's a hit on the search the whole store is listed, and I'm unable to figure out from which offer the match came from. 但是这样一来,只要搜索成功,就会列出整个商店,而我无法弄清楚匹配来自哪个报价。
Is there a way to do this with indexes on mongoose? 有没有办法用猫鼬的索引做到这一点? Figuring out which array element matched the query? 找出与查询匹配的数组元素?
I'm not sure that's possible with a $text
index. 我不确定$text
索引是否可行。
With a straight query you can use a projection to do the same: 对于直接查询,您可以使用投影来执行以下操作:
> db.array_find.find({ "offers.name": "firstname"},{"offers.$" : 1} )
But the text query doesn't reference the array directly so offers.name
is not able to be used in the projection. 但是文本查询不会直接引用数组,因此不能在投影中使用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
}
The problem with attempting any type of post processing of the array from a result document is your not going to be using mongo's text indexing but some approximation of it. 尝试对结果文档中的数组进行任何类型的后处理的问题是,您将不会使用mongo的文本索引,而是将其近似使用。
You may need a different data structure 您可能需要其他数据结构
_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.