如何在MongoDB中搜索对象数组

如何在MongoDB中搜索对象数组

要在MongoDB中搜索对象数组,可以使用$elemMatch操作符。该操作符允许我们从数组对象中搜索多个组件。

为了理解上述概念,让我们创建一个带有文档的集合。创建带有文档的集合的查询如下所示−

> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Adam","EmployeeLastName":"Smith","EmployeeDateOfBirth":new ISODate("1992-01-31 13:45:10"),
   ... "EmployeeSkills":["Spring and Hibernate Framework","Machine Learning"],
   ... "EmployeeDetails":[
      ... {
         ... "EmployeePerformanceArea":"Java",
         ... "Year":2001
      ... },
      ... {
      ... "EmployeePerformanceArea":"Python",
         ... "Year":2005
      ... }
   ... ]
... }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c77b3812386c62d05142a6a")
}
> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Carol","EmployeeLastName":"Taylor",
   "EmployeeDateOfBirth":new ISODate("1993-04-21 11:10:20"),
   "EmployeeSkills":["C++","Cloud Computing"], "EmployeeDetails":[ {
   "EmployeePerformanceArea":"C++", "Year":1998 }, { "EmployeePerformanceArea":"C++
   Game Developer", "Year":2007 } ] } );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c77b58f2386c62d05142a6b")
}

查看集合中的所有文档 借助 find() 方法 进行。查询如下 −

> db.searchArrayDemo.find().pretty();

以下是输出内容−

{
   "_id" : ObjectId("5c77b3812386c62d05142a6a"),
   "EmployeeFirstName" : "Adam",
   "EmployeeLastName" : "Smith",
   "EmployeeDateOfBirth" : ISODate("1992-01-31T13:45:10Z"),
   "EmployeeSkills" : [
      "Spring and Hibernate Framework",
      "Machine Learning"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "Java",
         "Year" : 2001
      },
      {
         "EmployeePerformanceArea" : "Python",
         "Year" : 2005
      }
   ]
}
{
   "_id" : ObjectId("5c77b58f2386c62d05142a6b"),
   "EmployeeFirstName" : "Carol",
   "EmployeeLastName" : "Taylor",
   "EmployeeDateOfBirth" : ISODate("1993-04-21T11:10:20Z"),
   "EmployeeSkills" : [
      "C++",
      "Cloud Computing"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "C++",
         "Year" : 1998
      },
      {
         "EmployeePerformanceArea" : "C++ Game Developer",
         "Year" : 2007
      }
   ]
}

在MongoDB中搜索对象数组的查询如下所示。

情况1

当找到给定元素时。

查询如下所示 −

> db.searchArrayDemo.find({EmployeeDetails:{$elemMatch:{EmployeePerformanceArea : "C++", Year : 1998}}}).pretty();

以下是输出结果−

{
   "_id" : ObjectId("5c77b58f2386c62d05142a6b"),
   "EmployeeFirstName" : "Carol",
   "EmployeeLastName" : "Taylor",
   "EmployeeDateOfBirth" : ISODate("1993-04-21T11:10:20Z"),
   "EmployeeSkills" : [
      "C++",
      "Cloud Computing"
   ],
   "EmployeeDetails" : [
      {
         "EmployeePerformanceArea" : "C++",
         "Year" : 1998
      },
      {
         "EmployeePerformanceArea" : "C++ Game Developer",
         "Year" : 2007
      }
   ]
}

第二种情况

当给定的元素未找到时。

查询如下所示 –

> db.searchArrayDemo.find({EmployeeDetails:{$elemMatch:{EmployeePerformanceArea : "C", Year : 1996}}}).pretty();

在找不到给定的元素时,您将不会获得任何内容

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程