我有一个查询,当我使用$ group时,错误显示“字段“ $ name必须是一个累加器对象”,如果删除归档的“ $ name”则一切正常,而我尝试仅使用“ name”的“ $ name”,错误继续。
User.aggregate([
{
$match: {
"storeKey": req.body.store
}
},
{
$group: {
"_id": "$_id",
"name": "$name",
"count": {
"$sum": 1
},
"totalValue": {
"$sum": "$value"
}
}
},
{
$sort: sort
},
{
$skip: req.body.limit * req.body.page
},
{
$limit: req.body.limit
}
])...
Run Code Online (Sandbox Code Playgroud) 我有一个查询,使用$ lookup “联接”两个模型,此后,我使用$ project选择所有需要的字段,但是$ project带来了很多对象(user_detail),其中包含我需要的更多数据。我只想要结果的两个字段(scheduleStart和scheduleEnd)。
我的查询:
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: …Run Code Online (Sandbox Code Playgroud) 我有两个模型,user 和 schedule,我想使用$lookup和 mongoose将这两个模型结合起来。
用户(型号)
name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number, …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
我有这个查询,并且正在使用$lookup,之后我想对value查找提供的数组中的求和。
我的查询(我正在使用猫鼬):
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{ $project: { "user_detail.value": 1 } },
])
Run Code Online (Sandbox Code Playgroud)
我的查询结果(样本集合):
{
"_id": {
"id": "5bd89899bda5c343749d00f0",
"name": "marcio",
"cpf": null,
"phone": "11999999999",
"email": "marcio@gmail.com",
"birthday": "2018-10-30T17:44:57.834Z"
},
"user_detail": [
{
"value": …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework