我有 2 個查詢:
aggregate( [
{
$match: {"user_id": 5918725, "shop_id": 1775207, "$text": {"$search" : "API"}}
},
{
$group: {
_id: "_id",
count: {
$sum: {
$cond: [ { $eq: [ "$deleted_at", null ] }, 1, 0 ]
}
}
}
}
]);
和
aggregate( [
{
$match: {"user_id": 5918725, "shop_id": 1775207, "$text": {"$search" : "APIU"}}
},
{
$group: {
_id: "_id",
count: {
$sum: {
$cond: [ { $eq: [ "$deleted_at", null ] }, 1, 0 ]
}
}
}
}
]);
they are the same, only difference is a search keyword, but they behave differently, in case of the first query, result is :
{
"_id" : null,
"count" : 0.0
}
which is expected result, but in case of the second one, result is Fetched 0 record(s) (that is what my GUI shows). So for some reason in case of the second query it simply ignore group/sum and trying to get actual records. How can I solve it, thank you in advance!
uj5u.com熱心網友回復:
詢問
- 在驅動程式上執行此操作的簡單方法,如果沒有結果,您可以使用代碼制作此檔案
- 這是在資料庫上執行此操作的方法,有 2 個額外的階段,聯合和組,以及一個額外的集合(在 mongodb 5.3 中,我們不需要額外的集合)
- 例如,而不是 5 一個負數,即使沒有結果你仍然得到
count : 0
測驗代碼在這里
aggregate(
[{"$match":{"$expr":{"$lt":["$a", 5]}}},
{"$group":{"_id":null, "count":{"$sum":1}}},
{"$unionWith":{"coll":"2"}},
{"$group":{"_id":null, "count":{"$sum":"$count"}}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438867.html
標籤:mongodb
