本文介绍了需要执行.toArray()以获取键名不是value的mongodb .find()的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在这个原始的问题我试图仅按键名进行搜索,并且仅输出该名称的值,如果我不这样做,则可以通过使用.toArray()来做到这一点,那么我将获得大量无关数据的输出.但是在回调中使用数据很麻烦,因为数据本来应该放在数组中.

Alright so in this original question i was trying to search by key name only and only outputting the value of that name i was able to do this by using .toArray() if i did not then i would get this huge output of irrelevant data. But then it's tedious in the callback to use the data because it was in a array when it's not supposed to be.

一些例子.两者之间有什么区别.前者给了我一个输出而没有使用.toArray(),后者给了我,如果我删除了.toArray(),我得到了不相关的数据作为输出.

Some examples. What is the difference between these two. The former gives me a output with out the use of .toArray() and the latter if i remove the .toArray() i get irrelevant data as the output.

前者

collection.findOne({"username" : username}, function(err, result) {
            console.log(result);
            callback(err, result);
            db.close();
        });

后期

collection.find({},{"credentials":1}).toArray(function(err, result) {
            callback(err, result);
            db.close();
        });

为什么还包含一个以上的参数,例如 "credentials":1,"_id":0,"username":0 ,就像原始问题一样,用户Alok Deshwal回答了吗?

Also why when more then one parameter is included i.e "credentials":1,"_id":0,"username":0 as in the original question a answer by user Alok Deshwal do you get the error.

MongoError:无法规范化查询:BadValue Projection无法混合包含和排除.

MongoError: Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.

所以我想最重要的是如何在不使用 .toArray()

So i guess the bottom line is how do i achieve a output with out the use of .toArray()

我正在使用.toArray()输出.

Output I'm getting with .toArray().

[{
    _id: 5636e00c09431db4560ef063,
    credentials: {
        password: '120bfeae7386165304b1cce4755a5c509593cc9157f58bac9d6f03e2230421cf',
        randomSalt: '00dfb37635ba7e5a513f9fd6e8bdf746f85ec3571df8288e1fdb44f399e331f0'
    }
}]

我想要的输出.

{
    _id: 56378e258300a47301b151ed,
    credentials: {
        password: '05c9f953969c7478fab0c5495b50d356ae9205c62ff41bab4d7891b804c1f369',
        randomSalt: '09973bc8109a9f6255f63e96528f5cf8ef74248192c60121159781f5d1f5f264'
    }
}

推荐答案

该错误表示您无法混合包含和排除键(不考虑_id).确切地说,您可以继续

That error means you can not mix inclusion and exclusion of keys(_id not considered). To be precise you can go on with

"credentials":0,"_id":0,"username":0

"credentials":1,"_id":0,"username":1

不允许混合包含和排除.您可以参考 https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/.来到第二部分,cursor.toArray()的替代方法是使用next

Mixing of inclusions and exclusions is not allowed.You can refer to https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ for that. Coming to second part alternatives for cursor.toArray() are using next

cursor.next(function(err,result){
   if (result)
      //Returns the next document in a cursor.
})

或使用以下每种方法

cursor.each(function(err,result){
    if(result){
       //each document in cursor
    }
})

这篇关于需要执行.toArray()以获取键名不是value的mongodb .find()的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:55