通过词语查找 MongoDB 文档
如需通过词语查找 MongoDB 文档,请使用 find() 并设置词语,如下所示 −
word/i
让我们创建一个包含文档的集合 −
> db.demo212.insertOne({"details":[{"Name":"John Doe"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e2c7603d395bdc21346ff") } > db.demo212.insertOne({"details":[{"Name":"Chris Brown"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e2c8003d395bdc2134700") } > db.demo212.insertOne({"details":[{"Name":"Robert doe"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e2c8a03d395bdc2134701") }
在集合中显示所有文档,借助 find() 方法 −
> db.demo212.find();
这会产生以下输出 −
{ "_id" : ObjectId("5e3e2c7603d395bdc21346ff"), "details" : [ { "Name" : "John Doe" } ] } { "_id" : ObjectId("5e3e2c8003d395bdc2134700"), "details" : [ { "Name" : "Chris Brown" } ] } { "_id" : ObjectId("5e3e2c8a03d395bdc2134701"), "details" : [ { "Name" : "Robert doe" } ] }
以下是通过词语查找 MongoDB 文档的查询 −
> db.demo212.find({"details.Name":/doe/i});
这会产生以下输出 −
{ "_id" : ObjectId("5e3e2c7603d395bdc21346ff"), "details" : [ { "Name" : "John Doe" } ] } { "_id" : ObjectId("5e3e2c8a03d395bdc2134701"), "details" : [ { "Name" : "Robert doe" } ] }
广告