将NumberInt()
被用于明确指定的32位整数。让我们创建一个包含文档的集合-
> db.demo357.insertOne( ... { ... "FirstName" : "Chris", ... "Age" : 21, ... "details" : { ... "studentDetails" : { ... "id" : NumberInt(101) ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e568fa6f8647eb59e5620c9") } > db.demo357.insertOne( ... { ... "FirstName" : "David", ... "Age" : 23, ... "details" : { ... "studentDetails" : { ... "id" : NumberInt(110) ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e568fbaf8647eb59e5620ca") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo357.find();
这将产生以下输出-
{ "_id" : ObjectId("5e568fa6f8647eb59e5620c9"), "FirstName" : "Chris", "Age" : 21, "details" : { "studentDetails" : { id" : 101 } } } { "_id" : ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName" : "David", "Age" : 23, "details" : { "studentDetails" : { "id" : 110 } } }
这是获取特定文档的查询-
> db.demo357.find({"details.studentDetails.id":NumberInt(110)});
这将产生以下输出-
{ "_id" : ObjectId("5e568fbaf8647eb59e5620ca"), "FirstName" : "David", "Age" : 23, "details" : { "studentDetails" : { "id" : 110 } } }