投影无法在mongo中与db.collection.find一起使用 [英] projection not working with db.collection.find in mongo
本文介绍了投影无法在mongo中与db.collection.find一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一天前,我已经开始使用mongodb,并且遇到了问题. 我在net和stackoverflow上搜索了如何在最终答案中隐藏_id值,并按照提供的答案进行了尝试,使我的代码得以运行,但_id部分仍然显示.
I have started to use mongodb just a day back and have encountered an issue. I searched on net and stackoverflow for how to hide _id value in final answer and following the answers provided I tried to get my code run but still the _id part shows.
P.S .:我正在使用cloud9作为想法.
P.S.: I am using cloud9 as the ide.
var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://localhost:27017/learnyoumongo', function(err, database) {
if(err) throw err;
const db = database.db('learnyoumongo');
var parrots = db.collection('parrots');
parrots.find({
age: { $gt: +process.argv[2] }
},{
name: 1,
age: 1,
_id: 0
}).toArray(function(err, docs){
if(err) throw err;
console.log(docs);
database.close();
});
});
推荐答案
您可以这样分离投影:
parrots.find({
age: { $gt: +process.argv[2] }
}).project({_id:0}).toArray(function(err, docs){
if(err) throw err;
console.log(docs);
database.close();
});
我遇到无法投影的问题,上述方法对我有效
i had the same issue with being unable to get projection to work, and the above method worked for me
这篇关于投影无法在mongo中与db.collection.find一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文