使用 NumberInt() 设置字段值来获取特定文档的 MongoDB 查询?
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 } } }
广告