如何在Node.js中进行MongoDB聚合 [英] How to MongoDB aggregation in Node.js

查看:95
本文介绍了如何在Node.js中进行MongoDB聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var mongoose = require('mongoose');
var membersModel = require('./model_member.js');
exports.request_friend_list = function(req, res){
var userid=req.body.userid;
console.log(userid);

//  membersModel.find({_id: userid,friends:{status:0}},{_id:0,'friends':1,},function(err,data)
    membersModel.aggregate(
         {$match: {_id:userid}},
         {$project: {friends: 1}},
         {$unwind: "$friends"},
         {$match: {"friends.status": 0}}
     ,function(err,data){
     if(err){
        res.json({status:"error"});
        throw err;
    }else{
        if(JSON.stringify(data).length > 0){
            console.log(JSON.stringify(data));
            res.json(data);
        }
        else{
            res.json({status: "Data is not Exist."});
            console.log("Data is not Exist.");
        }
    }   
});

membersModel.find({...})正常运行,但是memberModel.Aggregation({...})无法正常工作.这在MongoDB中也适用:

membersModel.find({...}) is operating normally, but memberModel.Aggregation({...}) is not working. This also works in MongoDB:

db.members.aggregate({$match:_id: ObjectId("532b4729592f81596d000001"),$project:"friends":1,$unwind:"$friends",$match:"friends.status": 0})

出什么问题了?

推荐答案

这里可能的问题是,当userid值传递到管道中时,它实际上不是正确的ObjectID类型.这导致初始阶段没有任何匹配".

The likely problem here is that your userid value is not actually a correct ObjectID type when it is being passed into the pipeline. This results in nothing being "matched" in the initial stage.

因此,作为更完整的示例:

Therefore as a more complete example:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectID = require("mongodb").ObjectID;

mongoose.connect("mongodb://localhost/test");

friendSchema = new Schema({
  "name": String,
  "status": Number
});

memberSchema = new Schema({
  friends: [friendSchema]
});

var Member = mongoose.model("Members", memberSchema );

var userid = new ObjectID("537ec520e98bcb378e811d54");

console.log( userid );

Member.aggregate([
  { "$match": { "_id": userid } },
  { "$unwind": "$friends" },
  { "$match": { "friends.status": 0 } }],
  function( err, data ) {

    if ( err )
      throw err;

    console.log( JSON.stringify( data, undefined, 2 ) );

  }
);

然后将按预期匹配数据:

Which then will match data as expected:

[
  {
    "_id": "537ec520e98bcb378e811d54",
    "friends": [{
      "name": "Ted",
      "status": 0
    }]
  }
]

因此请小心确保其类型正确.当在管道阶段针对_id提到聚合方法时,聚集方法不会自动将诸如"537ec520e98bcb378e811d54"之类的字符串值自动包装为ObjectID类型,就像Mongoose用其他查找和更新方法那样.

So be careful to make sure this is of the correct type. The aggregate method does not automatically wrap a string value such as "537ec520e98bcb378e811d54" into an ObjectID type when it is mentioned in a pipeline stage against _id in the way that Mongoose does this with other find and update methods.

这篇关于如何在Node.js中进行MongoDB聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆