让我们创建一个包含文档的集合-
> db.demo331.insertOne({"Words":"This is a MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c35f8647eb59e562089") } > db.demo331.insertOne({"Words":"THIS is a MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c36f8647eb59e56208a") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo331.find();
这将产生以下输出-
{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" } { "_id" : ObjectId("5e521c36f8647eb59e56208a"), "Words" : "THIS is a MongoDB" }
以下是为文本搜索创建索引的查询-
> db.demo331.createIndex( {Words: "text" } ); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo331.find({$text:{$search:"MySQL"}});
这将产生以下输出-
{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" }