在 MongoDB 中搜索哈希数组?
让我们首先创建一个包含文档的集合 -
> db.searchAnArrayDemo.insertOne({_id:1,"TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 1 } > db.searchAnArrayDemo.insertOne({_id:2,"TechnicalDetails":[{"Language":"MySQL"}]}); { "acknowledged" : true, "insertedId" : 2 } > db.searchAnArrayDemo.insertOne({_id:3,"TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 3 } > db.searchAnArrayDemo.insertOne({_id:4,"TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 4 } > db.searchAnArrayDemo.insertOne({_id:5,"TechnicalDetails":[{"Language":"Java"}]}); { "acknowledged" : true, "insertedId" : 5 }
以下是通过 find() 方法从集合中显示所有文档的查询 -
> db.searchAnArrayDemo.find().pretty();
这将产生以下输出 -
{ "_id" : 1, "TechnicalDetails" : [ { "Language" : "MongoDB" } ] } { "_id" : 2, "TechnicalDetails" : [ { "Language" : "MySQL" } ] } { "_id" : 3, "TechnicalDetails" : [ { "Language" : "MongoDB" } ] } { "_id" : 4, "TechnicalDetails" : [ { "Language" : "MongoDB" } ] } { "_id" : 5, "TechnicalDetails" : [ { "Language" : "Java" } ] }
以下是搜索哈希数组的查询 -
> db.searchAnArrayDemo.find({'TechnicalDetails.Language':'MongoDB'}).count();
这将产生以下输出 -
3
广告