【问题标题】:Mongo find by substring in any field [duplicate]Mongo在任何字段中通过子字符串查找[重复]
【发布时间】:2016-11-18 11:03:24
【问题描述】:

如果该子字符串可能包含在该集合的任何字段中,是否可以通过某个子字符串在集合中找到对象。

例如,我的子字符串是 "aa"。 我有 2 个收藏:

{
    "_id" : ObjectId("5788a3e4ffcf140b5955eead"),
    **"Address" : "aaBB",**
    "FirstName" : "First",
    "SecondName" : "Second"
} 

{
    "_id" : ObjectId("7788a3e4fhcf140098725eead"),
    "Address" : "bb",
    "FirstName" : "First",
    **"SecondName" : "aaAA"**
} 

那么,我应该使用什么查询来获取这 2 条记录?

谢谢

【问题讨论】:

  • @chridam ,我需要搜索集合中的所有字段。但在这个答案中,它是由某个字段 A 搜索的
  • $or 与正则表达式一起使用,即db.collection.find({ "$or": [ { "Address": /aa/ }, { "SecondName": /aa/ } ] })
  • @SSDMS 听起来很理想,将重新打开但不能再次关闭,因为这样做的投票将被消耗。

标签: mongodb mongodb-query nosql


【解决方案1】:

我找到了适合我的情况的解决方案:

db.getCollection('ContractAnswers').find({$where:"JSON.stringify(this).indexOf('aa')!=-1"})

【讨论】:

  • 我的女孩!它有效..
猜你喜欢
  • 2014-05-09
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2014-02-27
  • 2013-09-27
  • 1970-01-01
相关资源
最近更新 更多