【发布时间】:2015-08-29 04:59:27
【问题描述】:
我正在尝试查询字段等于某个值的 mongo db。
db.votes.find( { "userID": "a6MtLKmDYbiKXccXC" }, { name: 1 } );
在 mongo shell 中返回我需要的内容。
当我在我的 javascript 中尝试以下操作时,它返回一个空数组。
var seen = Votes.find({ "userID": "a6MtLKmDYbiKXccXC" }, { name: 1 }).fetch();
Meteor.publish("votes", function(args) {
var sub = this;
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
var pipeline = [
{ "$group": {
"_id": "$name",
"likes": { "$sum": { "$cond": [{ "$eq": [ "$vote", 1 ] },1,0] } },
"dislikes": { "$sum": { "$cond": [{ "$eq": [ "$vote", 2 ] },1,0] } },
"total": { "$sum": { "$cond": [{ "$eq": [ "$vote", 1 ] },1,-1] } }
}},
{ "$sort": { "total": -1, "_id": 1 } }
];
db.collection("votes").aggregate(
pipeline,
Meteor.bindEnvironment(
function(err, result) {
_.each(result, function(e) {
e.name = e._id;
delete e._id;
sub.added("cardStore",Random.id(), e);
});
sub.ready();
},
function(error) {
Meteor._debug( "error running: " + error);
}
)
);
});
【问题讨论】:
-
您是否向客户发布了投票?
Votes.find().count()从 Web 控制台返回什么? -
它应该返回 14