0
{
useLimit: Number,
uses: [{...}]
}
我想查詢這個集合僅匹配的那些文件,其中的用途的長度小於或useLimit是useLimit -1 (代表無限)做這件事的
{
useLimit: Number,
uses: [{...}]
}
我想查詢這個集合僅匹配的那些文件,其中的用途的長度小於或useLimit是useLimit -1 (代表無限)做這件事的
的一種方式,是利用$where
運營商在find()
查詢:
// pass a function to the `$where` operator, to pick records
// if they meet the desired condition.
db.collection.find({
$where : function() {
return (this.uses.length < this.useLimit || this.useLimit == -1);
}
})
或全光照克aggregation
管道,
isValid
,以其值解析 到true
或false
。 true
,如果條件符合,則返回false
。isValid
屬性的值匹配,值爲true
。$project
運營商投射除 isValid
以外的字段。如下,
db.collection.aggregate([ {
$project : {
"uses" : 1,
"useLimit" : 1,
"isValid" : {
$cond : [ {
$or : [ {
$gte : [ "$useLimit", {
$size : "$uses"
} ]
}, {
$eq : [ "$useLimit", -1 ]
} ]
}, true, false ]
}
}
}, {
$match : {
"isValid" : true
}
}, {
$project : {
"uses" : 1,
"useLimit" : 1
}
} ])