假设我有一个包含以下内容的文档:
var userSchema = Schema({
_id: Number,
fName: String,
lName: String,
email: String,
braintreeflag : {type:Boolean, default: false},
balance: {type:Number, default: 0},
braintreeId : {unique:true, type:Number, default:0},
notificationHubTag: String,
defaultPaymentMethod: Number,
braintreeTokens : [{id: Number, token : String}]
});
exports.getDefaultPaymentMethodToken = function(uId,defaultId, res){
//TODO query
}
我想在文档中查找_id:uID,我想找到id:defaultId的数组元素,然后返回此元素中的标记。看看SO,我发现了什么看起来是最好的解决方案,但没有经过测试:
User.findOne({_id:uId, 'braintreeTokens.id': defaultId}, {'braintreeTokens.$':1}, function(err,user){
})
答案 0 :(得分:0)
尝试:
db.collection.aggregate([
{$unwind:"$employees"},
{$match:{"employees.lastName":"Doe"}}
])
此查询将返回一个或多个lastName = "Doe"