让我们创建一个包含文档的集合-
> db.demo590.insert([ ... { "Name": "Chris", "Age": 21 }, ... {"Name": "Bob", "Age": 20}, ... { "Name": "Sam", "Age": 19 } ... ]); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
在find()方法的帮助下显示集合中的所有文档-
> db.demo590.find();
这将产生以下输出-
{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e92d514fd2d90c177b5bcd1"), "Name" : "Bob", "Age" : 20 } { "_id" : ObjectId("5e92d514fd2d90c177b5bcd2"), "Name" : "Sam", "Age" : 19 }
以下是显示所有字段值的查询,除了_id-
> var listOfObject = []; > db.demo590.find().forEach(function(d){ ... var ob = {}; ... ob["StudentFirstName"] = d.Name; ... ob["StudentAge"] = d.Age; ... listOfObject.push(ob); ... });
以下是显示文档的查询-
> listOfObject;
这将产生以下输出-
[ { "StudentFirstName" : "Chris", "StudentAge" : 21 }, { "StudentFirstName" : "Bob", "StudentAge" : 20 }, { "StudentFirstName" : "Sam", "StudentAge" : 19 } ]