【问题标题】:Querying nested documents in mongoDB查询 mongoDB 中的嵌套文档
【发布时间】:2011-09-22 19:46:16
【问题描述】:

我想查询我的 Mongodb 集合(名称:包装器)并检索所有具有以 '.com' 结尾的字段 'urls' 的文档

我不确定如何查询嵌套文档以及如何使用正则表达式进行查询。

我实际上是用 perl 编码的。但是,在 mongo shell 上运行的查询也可以。

提前致谢!

样本数据:

{ "_id" : ObjectId("4e7a34932cd4b16704000000"), "lastArray" : { "desc" : "google", "url" : "google.com", "data" : [
    {
        "name" : "1",
        "xpath" : [ ],
        "nodes" : [ ],
        "type" : "Any Text"
    },
    {
        "name" : "2",
        "xpath" : [ ],
        "nodes" : [ ],
        "type" : "Any Text"
    }



{ "_id" : ObjectId("4e7a34932cd4b16704000001"), "lastArray" : { "desc" : "yahoo", "url" : "yahoo.com", "data" : [
    {
        "name" : "1",
        "xpath" : [ ],
        "nodes" : [ ],
        "type" : "Any Text"
    },
    {
        "name" : "2",
        "xpath" : [ ],
        "nodes" : [ ],
        "type" : "Any Text"
    }

【问题讨论】:

    标签: perl mongodb


    【解决方案1】:
    db.wrappers.find({"lastArray.url":{$regex:/\.com$/}});
    

    【讨论】:

    【解决方案2】:

    我不能 100% 确定您是否可以使用正则表达式来搜索子文档属性,或者您是否出于性能原因甚至想要这样做。在这种情况下,添加“tld”属性并执行完全匹配查询可能会更容易。像这样 { "lastArray.tld" : "com" }

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 2021-02-25
      • 2016-08-16
      • 2023-03-08
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多