简体   繁体   English

通过高效的方式搜索对象列表mongo

[英]Search through list of objects in efficient way mongo

I have a json that looks a bit like this in meteor. 我有一个在流星中看起来像这样的json。

{
  "_id": "someid",
  "createdAt": ISODate("2015-03-10T14:26:02.430Z"),
  "emails": [{
      "address": "raven@corvid.com",
      "verified": false
    }, {
      "address": "crow@corvid.com",
       "verified": false
  }]
}

I want to search the database and find if anyone has a given email, say, crow@corvid.com . 我想搜索数据库,查找是否有人给定电子邮件,例如crow@corvid.com If it were just a static value, you could do Meteor.users.findOne({ email: "crow@corvid.com" }); 如果只是静态值,则可以执行Meteor.users.findOne({ email: "crow@corvid.com" }); , but the list makes it more complicated. ,但是列表使它更加复杂。

What is the efficient way to do this? 有效的方法是什么?

您可以使用$ elemMatch运算符执行此操作,以匹配包含crow@corvid.com作为地址值的文档。

Meteor.users.findOne({ emails: {$elemMatch: {address:"crow@corvid.com" }}})

您还可以为数据库建立索引,可以为嵌入的文档建立索引,您的案例是一个数组Meteor.users.createIndex( { "emails" : 1 } )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索 JavaScript 对象列表并确定它们是否包含相同值的最有效方法是什么? - What is the most efficient way to search through a list of JavaScript objects and determine whether they contain the same values? 更有效的搜索javascript对象数组的方法? - More efficient way to search an array of javascript objects? 更有效的方式搜索数组中的多个对象 - More efficient way search for multiple objects in array 什么是搜索食谱数组并匹配某些成分列表上的多个单词的有效方法 - What is an efficient way to search through an array of recipes and match multiple words on some ingredients list 搜索对象和数组定位匹配的最有效方法 - Most efficient way to search through an object and array locating match 从对象列表获取数据的最有效方法? - Most efficient way to obtain data from a list of objects? 什么是基于对象值将对象列表压缩为的有效方法? - What is a efficient way to condense a List of objects to based on an object value? Efficient.includes() 在对象数组中搜索 - Efficient .includes() search in array of objects 迭代 2 个对象数组并返回单个对象数组的有效方法是什么? - What's the efficient way to iterate through 2 arrays of Objects and return a single array of objects? 搜索包含多个对象和数组的对象的最佳方法 - Best way to search through an Object containing multiple objects and arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM