对于类似的操作,您可以使用/ searchLetter /。首先让我们创建一个包含文档的集合-
> db.demo26.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c9dc22d07d3b95082e79") } > db.demo26.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c9e022d07d3b95082e7a") } > db.demo26.insertOne({"StudentName":"Jones"}); { "acknowledged" : true, "insertedId" : ObjectId("5e14ca7222d07d3b95082e7b") } > db.demo26.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e14ca7622d07d3b95082e7c") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo26.find();
这将产生以下输出-
{ "_id" : ObjectId("5e14c9dc22d07d3b95082e79"), "StudentName" : "Chris" } { "_id" : ObjectId("5e14c9e022d07d3b95082e7a"), "StudentName" : "John" } { "_id" : ObjectId("5e14ca7222d07d3b95082e7b"), "StudentName" : "Jones" } { "_id" : ObjectId("5e14ca7622d07d3b95082e7c"), "StudentName" : "David" }
以下是在MongoDB中使用“ LIKE”语句的查询-
> db.demo26.find({"StudentName":/Jo/});
这将产生以下输出-
{ "_id" : ObjectId("5e14c9e022d07d3b95082e7a"), "StudentName" : "John" } { "_id" : ObjectId("5e14ca7222d07d3b95082e7b"), "StudentName" : "Jones" }