【问题标题】:Meteor collection select WHERE流星集合选择WHERE
【发布时间】: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

标签: mongodb meteor


【解决方案1】:

您使用的是 autopublish 软件包吗?如果没有,您需要在服务器端有发布方法并在客户端有相应的订阅调用,以确保您拥有运行此查询所需的数据。

Meteor Publish and Subscribe Documentation

我建议使用 msavin:mongol 包来调试浏览器中的数据和订阅。

旁注:如果要使用 1 的集合调用 fetch,则可以使用 findOne 作为简写。

var seen = Votes.findOne({ "userID": "a6MtLKmDYbiKXccXC" }, { name: 1 });

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多