提取特定字符串的 MongoDB 文档
要提取带有特定字符串的 MongoDB 文档,请在 MongoDB 中使用 $match。让我们通过文档创建一个集合 −
> db.demo424.insert( ... { ... ... "Information" : [ ... { ... id:10, ... Name:"Chris" ... }, ... { ... id:11, ... Name:"David" ... } ... ] ... } ... ) WriteResult({ "nInserted" : 1 }) > > db.demo424.insert( ... { ... ... "Information" : [ ... { ... id:101, ... Name:"Mike" ... }, ... { ... id:102, ... Name:"John" ... } ... ] ... } ... ) WriteResult({ "nInserted" : 1 })
借助 find() 方法显示集合中的所有文档 −
> db.demo424.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] } { "_id" : ObjectId("5e74eb9ebbc41e36cc3cae6b"), "Information" : [ { "id" : 101, "Name" : "Mike" }, { "id" : 102, "Name" : "John" } ] }
以下是提取特定字符串的 MongoDB 文档的查询 −
> db.demo424.aggregate( ... [ { $match : { "Information.Name": "Chris" } } ] ... );
这将产生以下输出 −
{ "_id" : ObjectId("5e74eb9dbbc41e36cc3cae6a"), "Information" : [ { "id" : 10, "Name" : "Chris" }, { "id" : 11, "Name" : "David" } ] }
广告