查询其子集时获取 MongoDB 中的记录
可以使用 $all 运算符。我们首先创建一个包含文档的集合 −
> db.subsetOfAnArrayDemo.insertOne({"StudentProgrammingSkills": ["Java","MongoDB","MySQL","C++","Data Structure","Algorithm","Python","Oracle","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9d1e1895c4fd159f80804") }
以下是查询,以用 find() 方法显示集合中的所有文档 −
> db.subsetOfAnArrayDemo.find().pretty();
这会产生以下输出 −
{ "_id" : ObjectId("5cb9d1e1895c4fd159f80804"), "StudentProgrammingSkills" : [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ] }
以下是获取数组子集的查询 −
> db.subsetOfAnArrayDemo.find({ StudentProgrammingSkills: { $all: [ 'MongoDB', 'MySQL' ] } } ).pretty();
这会产生以下输出 −
{ "_id" : ObjectId("5cb9d1e1895c4fd159f80804"), "StudentProgrammingSkills" : [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ] }
广告