2017-11-16 5 views
0

私はMongoDBとnode.jsの初心者です。実際には、特定のドキュメントの名前を取得しようとしていますが、その方法はわかります。 を「」「学生」と「学生」という名前の文書が含まれていますしようとすると、この["name","roll","age"]形式 でフィールド名を取得する方法を、次にnode.jsを使って特定のドキュメントのフィールド名(フィールド値ではない)のみを扱う方法

{ 
    "_id: "student", 
    "name": "alex", 
    "roll": "3", 
    "age":"13" 
} 

などの分野が含まれているコレクションの場合:私は詳細に詳しく説明しましょう これは:

db.collection("user").find({ "_id": "student" }).toArray(function(err, result) { 
    console.log(result) 
}) 

はすべてのフィールドに値を与えています。

答えて

0

試し:

db.collection("user").find({"_id":"student"}, {name:1}).toArray(function(err, result) {console.log(result)}

あなたがフィールドを除外したい場合は:

db.collection("user").find({"_id":"student"}, {roll:0, age:0}).toArray(function(err, result) {console.log(result)}

0

は、これは動作します:

db.collection("user",function(err,user){ 
    var array = []; 
    user.findOne(function(err,doc){ 
    for (key in doc) array.push(key); 
    }); 
    console.log(array); 
}); 
関連する問題