将查找查询中的结果猫鼬返回到变量 [英] Return results mongoose in find query to a variable

查看:25
本文介绍了将查找查询中的结果猫鼬返回到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 node.js 中使用 mongoose 返回查询结果.

I need to return the results of a query with mongoose in node.js.

如何返回值以将值设置为变量?

How do you return the value to set the value to a variable?

我需要做的是:

var results = users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
    if( err || !docs) {
        console.log("No user found");
    } else {            
        return docs;
    };
});

为了拥有:

results = docs 

非常感谢您的回复.

我还有一个问题.

如何使用 find 或 findOne 在查询运算符中传递变量?喜欢:

How to pass variable in a query operator with find or findOne? Like :

var foo = "Blaa";

users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) {
    if( err || !docs) {
        console.log("No user found");
    } else {
        // I want to use the foo variable here
        console.log(foo);
    };
});

推荐答案

有几种方法可以实现您想要的.

There are several ways to achieve what you want.

1.使用猫鼬查询

在这个策略中,您的函数返回一个 Mongoose 查询,您可以稍后使用它来调用方法 exec 并使用它来获取结果.

In this strategy, your function returns a Mongoose query which you can later use to invoke the method exec and use it to get the results.

function getJedisQuery(name){
   var query = Jedi.find({name:name});
   return query;
}

然后你可以简单地使用它:

Then you can use it simply doing:

var query =  getJedisQuery('Obi-wan');
query.exec(function(err,jedis){
   if(err)
      return console.log(err);
   jedis.forEach(function(jedi){
      console.log(jedi.name);
   });
});

2.使用 Mongoose Promise-like 对象

Moogose 为类似 promise 的对象提供支持.所有你需要做的事情有点类似于我上面所做的,但是这一次,你调用了 exec 方法而没有回调.

Moogose provides support for promise-like objects. All you have to do is something somewhat similar to what I did above, but this time, you invoke the exec method without a callback.

function getJedisPromise(name){
   var promise = Jedi.find({name:name}).exec();
   return promise;
}

然后你可以简单地使用它:

Then you can use it by simply doing:

var promise = getJedisPromise('Luke');
promise.then(function(jedis){
   jedis.forEach(function(jedi){
      console.log(jedi.name);
   });
})

正如本答案的评论部分所强调的,这些对象实际上并不是承诺,需要考虑到这一点(请参阅 查询不是承诺).

As highlighted in the comment section of this answer, these objects are not in fact promises and that needs to be taken into account (see Queries are not promises).

3.使用猫鼬流

最后,Mongoose 也支持流并且流是事件发射器.因此,您可以获得一个流,然后订阅数据"和错误"事件.像这样:

Finally, Mongoose has also support for streams and streams are event emitters. So, you could get a stream and then subscribe for 'data' and 'error' events. Like this:

function getjedisStream(name){
   var stream = Jedi.find({name:name}).stream();
   return stream;
}

然后你可以简单地做:

var stream = getJedisStream('Anakin');
stream.on('data', function(jedis){
   jedis.forEach(function(jedi){
      console.log(jedi.name);
   });
});
stream.on('error', function(error){
    console.log(error);
});

来源,供以后参考.

这篇关于将查找查询中的结果猫鼬返回到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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