如果我有一個Post具有兩個屬性的物件
Title字串型別IsHomePage布爾型別IsTagged布爾型別
當使用 mongodb 聚合框架時,$unionWith我可以在一次 DB 往返中選擇我需要的內容(所有檔案都IsHomePage設定為true,其余檔案的數量與屬性IsTagged設定為true.
db.collection.aggregate([
{
"$match": {
IsHomePage: true
}
},
{
"$unionWith": {
"coll": "collection",
"pipeline": [
{
"$match": {
IsHomePage: {
$ne: true
},
IsTagged: true
}
},
{
"$sample": {
"size": 50
}
}
]
}
}
])
對于較舊的 MongoDB 實體(版本低于 4.4),我如何才能實作相同的目標。
注意:UnionWith 是在 4.4 版本中公布的。
uj5u.com熱心網友回復:
詢問
- 這可以做聯合所做的事情,但是有 facet 的限制(每個結果陣列 <16 MB,第二個肯定是因為 $sample)
*如果它不是您想要的,如果您可以提供示例資料,以及撰寫查詢 測驗的預期結果。
測驗代碼在這里
aggregate(
[{"$facet":
{"home": [{"$match": {"$expr": {"$eq": ["$IsHomePage", true]}}}],
"notHome":
[{"$match":
{"$expr":
{"$and":
[{"$ne": ["$IsHomePage", true]}, {"$eq": ["$IsTagged", true]}]}}},
{"$sample": {"size": 50}}]}},
{"$project": {"union": {"$concatArrays": ["$home", "$notHome"]}}},
{"$unwind": {"path": "$union"}},
{"$replaceRoot": {"newRoot": "$union"}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314272.html
上一篇:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.exc.InvalidDefinitionException
