如果我有一個Post具有兩個屬性的物件
Title字串型別IsHomePage布爾型別IsTagged布爾型別
場景:檔案總數為 100。IsHomePage設定為trueis20 in total的80 in total檔案,其余(檔案)為屬性IsTagged設定為 的檔案true。
如何構建查詢以使用 IsHomePage 選擇所有 20 個以及 IsTagged 設定為 true 且限制為 50 的隨機檔案?
uj5u.com熱心網友回復:
您可以使用$unionWith來組合您的 2 個邏輯。
db.collection.aggregate([
{
"$match": {
IsHomePage: true
}
},
{
"$unionWith": {
"coll": "collection",
"pipeline": [
{
"$match": {
IsHomePage: {
$ne: true
},
IsTagged: true
}
},
{
"$sample": {
"size": 50
}
}
]
}
}
])
這是Mongo 游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314275.html
