簡體   English   中英

在數組中獲取與文本索引查詢匹配的元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM