我正在开发游戏应用程序。
在一种情况下,我需要在子数组(SelectionData)的所有元素中找到所有满足条件的文档。
//型号
import mongoose from 'mongoose';
const User_Selection = mongoose.Schema({
UserSelectionID: { type: String, default: "" },
SelectionData: [
{
_id: false,
match_id: { type: String, default: "" },
selected_option: { type: String, default: "" },
selected_points: { type: Number, default: 0 },
Whether_Points_Calculated: { type: Boolean, default: false },
Points_Collected: { type: Number, default: 0 }
}
],
Opponent_Details: {
USERID: { type: String, default: "" },
DisplayName: { type: String, default: "" }
},
Whether_Final_Points_Calculated: { type: Boolean, default: false },
Total_Game_Points: { type: Number, default: 0 },
Status: { type: Boolean, default: true },
created_at: { type: Date, default: null },
updated_at: { type: Date, default: null }
}, { collection: 'User_Selection' });
export default mongoose.model('User_Selection', User_Selection);
我希望在所有数组元素中找到所有满足条件的文档(“ SelectionData.Whether_Points_Calculated” == true)。
答案 0 :(得分:0)
应该是这样
const cursor = async() => {
return new Promise((resolve, reject) => {
User_Selection.find({
SelectionData: { Whether_Points_Calculated: true }
})
. then(result => {
resolve(result)
})
. catch(err => {
reject(err)
})
}
答案 1 :(得分:0)
这必须工作:
db.getCollection('User_Selection')。find({“ SelectionData”:{“ $ elemMatch”:{“ Whether_Points_Calculated”:true}}})
答案 2 :(得分:0)
以下脚本对我有用。
db.collection.find({
"SelectionData": {
"$not": {
"$elemMatch": {
"Whether_Points_Calculated": {
"$eq": false
}
}
}
}
})
游乐场link
答案 3 :(得分:0)
db.collection.aggregate([
{"$match":{"cards":{"$exists":true}}},
{$project: {
"output": {$filter: {
input: '$cards' ,
as: 'arrayElement',
cond: {"$and":[{$eq: ['$$arrayElement.Whether_Points_Calculated', true]}]}
}}
}}
])