我有一些收藏品可以将用户的购买物品存入商店以及他从朋友那里购买的喜欢和不喜欢的东西。集合字段看起来像这样:
{
"_id" : ObjectId("543e43cea7c481c4820ab014"),
"user" : "542ea90fbb1e37b09f660980",
"product" : "543e43cea7c481c4820ab013",
"store" : "542e6fb1a2a8a7a071490555",
"status" : "active",
"dislikes" : 1,
"likes" : 3,
"addedAt" : ISODate("2014-10-15T09:52:14.661Z")
}
现在,我想得到以下摘要:
对于#1,我做了:
[
{$group: {_id: '$user', rankDiff: {$sum: {$subtract: ['$likes', '$dislikes']}}}},
{$match : { _id: 'X'}}
],
林'得到适当的结果:
[ { " _id" :" 542ea90fbb1e37b09f660980", " rankDiff" :2 }]
但是当我试图通过以下方式获得#2时
[
{$group: {_id: '$user', rankDiff: {$sum: {$subtract: ['$likes', '$dislikes']}}}},
{$match : { _id: 'X'}, store: 'Y'}
],
或者通过以下方式获得#3:
[
{$group: {_id: '$user', rankDiff: {$sum: {$subtract: ['$likes', '$dislikes']}}}},
{$match : { _id: 'X'}, store: 'Y', product: 'Z'}
],
没有结果返回。
有人能帮助我吗?我做错了什么?
P.S。由Mongoose完成,但也通过我的MongoDB实例直接与Robomongo一起完成
答案 0 :(得分:0)
找到它,查询语法应为:
[
{$match : { user: 'X', store: 'Y', product: 'Z'},
{$group: {_id: '$user', rankDiff: {$sum: {$subtract: ['$likes', '$dislikes']}}}}
]
现在工作正常。
谢谢!