gpt4 book ai didi

Use $search instead of $match when using $in conditions in MongoDB Atlas Search(在MongoDB Atlas Search中使用$in条件时,请使用$Search而不是$Match)

转载 作者:bug小助手 更新时间:2023-10-28 13:30:36 33 4
gpt4 key购买 nike



During a recent training, I learned about the benefits of using the $searchoperator.
I would like to modify my $match query to use $search instead.
However, I couldn't find an equivalent for the $in operator.

在最近的一次培训中,我了解了使用$earch操作符的好处。我想修改我的$Match查询以使用$Search。但是,我找不到与$in运算符等效的运算符。


When the fields need to match a single ObjectId, there is no problem, I can use the compound operator with must + equals and it works.

当字段需要匹配单个对象ID时,这是没有问题的,我可以使用带有MASH+EQUALS的复合运算符,它是有效的。


The problem arises when I have multiple ObjectId values.
Again, if it's only for one field, I could use should + equals while specifying minimumShouldMatch: 1.

当我有多个OBJECTID值时,问题就出现了。同样,如果它只用于一个字段,我可以在指定minumShouldMatch:1时使用Share+Equals。


But I'm not sure how to handle it if I have multiple fields with multiple ObjectId values...
How should I approach transforming a query like this using the $search operator?

但如果我有多个具有多个OBJECTID值的字段,我不确定如何处理它……我应该如何使用$Search操作符转换这样的查询?


 { '$match':
{ '$and':
[ { customer:
{ '$in': [ 5f3d6c51f5b2ed74fe93dd60, 5c221b4e4d17f1734806b4a5 ] } },
{ carrier: { '$in': [ 'carrier1', 'carrier2', 'carrier3' ] } },
{ status: { '$in': [ 'late', 'idle', 'delivered-late' ] } },
{ createDate: { '$gte': 2023-06-01T04:00:00.000Z } },
{ createDate: { '$lte': 2023-07-01T03:59:59.999Z } } ] }
}

更多回答
优秀答案推荐

You can either use the filter option in the $search query

您可以在$SEARCH查询中使用过滤器选项


   $search: {
"compound": {
"filter": [{
"queryString": {
"defaultPath": "role",
"query": "CLIENT OR PROFESSIONAL"
}
}]
}
}

Or you can use the $in operator like mentioned here in the docs but it only allows certain data types

或者你可以使用$in操作符,就像在文档中提到的那样,但它只允许某些数据类型


https://www.mongodb.com/docs/upcoming/reference/operator/query/in/#mongodb-query-op.-in

Https://www.mongodb.com/docs/upcoming/reference/operator/query/in/#mongodb-query-op.-in


更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com