node.js Mongoose:如何使用聚合并一起查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42394902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose: how to use aggregate and find together
提问by Colin Witkamp
How can I use aggregateand findtogether in Mongoose?
i.e I have the following schema:
如何在 Mongoose 中使用聚合和find一起?
即我有以下架构:
const schema = new Mongoose.Schema({
created: { type: Date, default: Date.now() },
name: { type: String, default: 'development' }
followers: [{ type: Mongoose.Schema.ObjectId, ref: 'Users'}]
...
})
export default Mongoose.model('Locations', schema)
How can I query the users with only the fields nameand followers_count.followers_count: the length of followers.
如何仅使用字段name和followers_count. followers_count: 的长度followers。
There, I know we can use selectto get only the field name.
How can we get the count of followers?
在那里,我知道我们可以使用select只获取 field name。
我们怎样才能得到计数followers?
回答by chridam
For MongoDB 3.6 and greater, use the $exproperator which allows the use of aggregation expressions within the query language:
对于 MongoDB 3.6 及更高版本,使用$expr允许在查询语言中使用聚合表达式的运算符:
var followers_count = 30;
db.locations.find({
"$expr": {
"$and": [
{ "$eq": ["$name", "development"] },
{ "$gte": [{ "$size": "$followers" }, followers_count ]}
]
}
});
For non-compatible versions, you can use both the $matchand $redactpipelines to query your collection. For example, if you want to query the locationscollection where the name is 'development' and followers_countis greater than 30, run the following aggregate operation:
对于不兼容的版本,您可以同时使用$match和$redact管道来查询您的集合。例如,如果要查询locations名称为 'development' 且followers_count大于 30 的集合,请运行以下聚合操作:
const followers_count = 30;
Locations.aggregate([
{ "$match": { "name": "development" } },
{
"$redact": {
"$cond": [
{ "$gte": [ { "$size": "$followers" }, followers_count ] },
"$$KEEP",
"$$PRUNE"
]
}
}
]).exec((err, locations) => {
if (err) throw err;
console.log(locations);
})
or within a single pipeline as
或在单个管道中作为
Locations.aggregate([
{
"$redact": {
"$cond": [
{
"$and": [
{ "$eq": ["$name", "development"] },
{ "$gte": [ { "$size": "$followers" }, followers_count ] }
]
},
"$$KEEP",
"$$PRUNE"
]
}
}
]).exec((err, locations) => {
if (err) throw err;
console.log(locations);
})
The above will return the locations with just the _idreferences from the users. To return the users documents as means to "populate" the followers array, you can then append the $lookuppipeline.
以上将仅返回_id用户引用的位置。要返回用户文档作为“填充”追随者数组的手段,您可以附加$lookup管道。
If the underlying Mongo server version is 3.4 and newer, you can run the pipeline as
如果底层 Mongo 服务器版本为 3.4 及更高版本,则可以将管道运行为
let followers_count = 30;
Locations.aggregate([
{ "$match": { "name": "development" } },
{
"$redact": {
"$cond": [
{ "$gte": [ { "$size": "$followers" }, followers_count ] },
"$$KEEP",
"$$PRUNE"
]
}
},
{
"$lookup": {
"from": "users",
"localField": "followers",
"foreignField": "_id",
"as": "followers"
}
}
]).exec((err, locations) => {
if (err) throw err;
console.log(locations);
})
else you would need to $unwindthe followers array before applying $lookupand then regroup with $grouppipeline after that:
否则,您需要$unwind在应用之前使用 follower 数组$lookup,然后$group在此之后使用管道重新组合:
let followers_count = 30;
Locations.aggregate([
{ "$match": { "name": "development" } },
{
"$redact": {
"$cond": [
{ "$gte": [ { "$size": "$followers" }, followers_count ] },
"$$KEEP",
"$$PRUNE"
]
}
},
{ "$unwind": "$followers" },
{
"$lookup": {
"from": "users",
"localField": "followers",
"foreignField": "_id",
"as": "follower"
}
},
{ "$unwind": "$follower" },
{
"$group": {
"_id": "$_id",
"created": { "$first": "$created" },
"name": { "$first": "$name" },
"followers": { "$push": "$follower" }
}
}
]).exec((err, locations) => {
if (err) throw err;
console.log(locations);
})
回答by Israel Zinc
You can use as the following:
您可以使用如下:
db.locations.aggregate([
{$match:{"your find query"}},
{$project:{"your desired fields"}}
])
In the match you can do stuff like:
在比赛中,您可以执行以下操作:
{{$match:{name:"whatever"}}
In the project, you can select the fields you want using numbers 0 or 1 like:
在项目中,您可以使用数字 0 或 1 选择所需的字段,例如:
{$project:{_id:1,created:0,name:1}}
Which 0 means, do not put and 1 means put.
其中0表示不放,1表示放。

