开发者

Querying nested documents in mongoDB

开发者 https://www.devze.com 2023-04-06 11:40 出处:网络
I want to query my Mongodb collection (name: wrappers) and retrieve all documents that have field \'urls\' that end with \'.com\'

I want to query my Mongodb collection (name: wrappers) and retrieve all documents that have field 'urls' that end with '.com'

I am not sure on how to query nested documents and also using regex for queries.

I am actually coding it in perl. However queries to run on the mongo shell would also be fine.

Thanks in advance!

Sample Data:

{ "_id" : ObjectId("4e7a34932cd4b16704000000"), "lastArray" : { "desc" : "google", "url" : "google.com", "data" : [
    {
        "name" : "1",
        "xpath" : [ ],
        "nodes" : [ ],
        "type" : "Any Text"
    },
    {
        "name" : "2",
        "xpath" : [ ],
        "nodes" : [ ],
    开发者_如何学C    "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"
    }


db.wrappers.find({"lastArray.url":{$regex:/\.com$/}});


I'm not 100% sure if you can use regex to search sub-document attributes or if you'd even want to for performance reasons. In a case like this, it might be easier to add a "tld" attribute and do an exact match query. Something like this { "lastArray.tld" : "com" }

0

精彩评论

暂无评论...
验证码 换一张
取 消