如何在MongoDB中查询所有项?
若要查询所有项,请使用find()。我们首先创建一个包含文档的集合−
> db.queryAllItemsDemo.insertOne({"StudentDetails":{"StudentName":"John","StudentSubject":["MongoDB","MySQL"],"StudentSubjectPrice":[4000,6000]},"OtherDetails":{"UserAge":29,"UserCountryName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5cef74ecef71edecf6a1f69f") }
在find()方法的帮助下显示集合中的所有文档−
> db.queryAllItemsDemo.find().pretty();
这将产生以下输出−
{ "_id" : ObjectId("5cef74ecef71edecf6a1f69f"), "StudentDetails" : { "StudentName" : "John", "StudentSubject" : [ "MongoDB", "MySQL" ], "StudentSubjectPrice" : [ 4000, 6000 ] }, "OtherDetails" : { "UserAge" : 29, "UserCountryName" : "US" } }
以下是如何在MongoDB中查询所有项−
> db.queryAllItemsDemo.find({},{StudentDetails: 1}).pretty();
这将产生以下输出−
{ "_id" : ObjectId("5cef74ecef71edecf6a1f69f"), "StudentDetails" : { "StudentName" : "John", "StudentSubject" : [ "MongoDB", "MySQL" ], "StudentSubjectPrice" : [ 4000, 6000 ] } }
广告