mark
mark

Reputation: 737

(mongodb) How to find null values inside embedded document

I want to search for this part:

"UrunAdi" : {
                "value" : "Büyük Anne 23 cm Vinil El Kuklası",
                "detail" : {
                    "revisedBy" : "CTA",
                    "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
                }
            },

urunadı means product name.

So if productname value is null or empty, i want to see as output.

I tried a few, all give wrong syntax errors.

I tried this to match ( then i would do for null if this worked)

db.products.find({fields:{$elemMatch:       "UrunAdi" : {
            "value" : "Vinil El Kuklası (23 cm Tilki)",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.410Z")
            }}}})

products is collection inside db product.

or ths failed

db.products.find({fields:{$elemMatch:      "UrunAdi" : {
           $elemMatch:   "value" : "Vinil El Kuklası (23 cm Tilki)",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.410Z")
            }}}})

This is long version of code. I get this by writing

first

use product

then

db.products.find().pretty()







{
    "_id" : ObjectId("57a21569c01432434b6f302b"),
    "_class" : "net..product.domain.Product",
    "status" : "NEW",
    "isValid" : false,
    "isMatched" : true,
    "isAnalyzed" : true,
    "hasValidImage" : true,
    "preMatchedSku" : "HBV000000DED8",
    "merchantSku" : "VRDYW-7320-BA",
    "merchant" : "46dc04b1-a39f-40fb-9697-d4b06f837952",
    "fields" : {
        "YasAraligi" : {
            "value" : "3 yaş üzeri",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("2016-08-03T16:01:45.720Z")
            }
        },
        "renk_varyant_property" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("1970-01-01T00:00:00Z")
            }
        },
        "UrunAciklamasi" : {
            "value" : "Çocuklarınıza \"evvel zaman içinde kalbur saman içinde\" ile başlayan hikayeler anlatmak yada onların daha eğlenceli vakit geçirmesini sağlamak için tam aradığınız oyuncak.<div><br></div><div>23 cm vinil el kuklası ile çocuklarınıza bir şeyleri paylaşabilme, beden dilini ve sesini kullanabilme özelliklerini kazandırabilirsiniz. Gerektiğinde kutusunda muhafaza edebilirsiniz.</div><br>Ürün Grubu : El Kuklası<br>Cinsiyet : Kız çocuk<br>Yaş Grubu : 3 yaş üzeri<br>Materyal : Vinil, Keçe<br>Ürün Ölçüsü : 23 cm ölçülerinde<br>Tema : Masal kahramanları<br>",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "hbSku" : {
            "value" : "HBV000000DED8",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "secenek-6_varyant_property" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("1970-01-01T00:00:00Z")
            }
        },
        "TaxVatRate" : {
            "value" : "18",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Image5" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Image4" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Image3" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Image2" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Kur" : {
            "value" : "TL",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Image1" : {
            "value" : "http://www.puzmo.com.tr/panel/UrunResimleri/puzmo/buyuk-anne-23-cm-vinil-el-kuklasi-80.jpg",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "tipi_varyant_property" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("1970-01-01T00:00:00Z")
            }
        },
        "VaryantID" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "yas_varyant_property" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("1970-01-01T00:00:00Z")
            }
        },
        "UrunAdi" : {
            "value" : "Büyük Anne 23 cm Vinil El Kuklası",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "StokAdedi" : {
            "value" : "1",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Cinsiyet" : {
            "value" : "Kız",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("2016-08-03T16:01:45.720Z")
            }
        },
        "Barcode" : {
            "value" : "2454750265123",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Kg" : {
            "value" : "1",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "olcu_varyant_property" : {
            "value" : "",
            "detail" : {
                "revisedBy" : "MERCHANT",
                "revisionDate" : ISODate("1970-01-01T00:00:00Z")
            }
        },
        "GarantiSuresi" : {
            "value" : "0",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        },
        "Marka" : {
            "value" : "Vardem",
            "detail" : {
                "revisedBy" : "CTA",
                "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
            }
        }
    },
    "listingStatus" : "FAILED",
    "uploadDate" : ISODate("2016-08-03T16:01:00Z"),
    "createdAt" : ISODate("2016-08-03T16:01:45.811Z"),
    "createdBy" : "user-0",
    "modifiedAt" : ISODate("2016-10-19T07:16:16.464Z"),
    "modifiedBy" : "root",
    "type" : DBRef("productTypes", ObjectId("575fd99236623f70c95924c2")),
    "tasks" : [ ]
}

I merged 2 answers and this worked produp:PRIMARY> db.products.find({"fields.UrunAdi.value":""}).count() 129855 but it is giving for all products. but i want for each merchant. in sql, we can use distinct for that. db.products.distinct( "merchant" ) is true for this but how can i combine those two queries?

Upvotes: 1

Views: 689

Answers (2)

Shrabanee
Shrabanee

Reputation: 2766

Try the following query:

db.products.find({$or:[
  {'fields.UrunAdi':{}, 
  {'fields.UrunAdi':{$exists:fasle},
  {'fields.UrunAdi':null}
]})

The above query will return the records where fields.UrunAdi value is null or fields.UrunAdi does not exists or is an empty object.

EDIT:-

Use aggregate like below:

db.products.aggregate([{$group: { _id:$merchant}},
  {$match:{
    $or:[
     {'fields.UrunAdi':{}, 
     {'fields.UrunAdi':{$exists:fasle},
     {'fields.UrunAdi':null}]
}}])

Refer doc-aggregate for more info.

Hope this will help.

Upvotes: 1

harry-potter
harry-potter

Reputation: 2049

Your fields field is not an array but it just contains some documents. If you want to find all documents that match this:

"UrunAdi" : {
                "value" : "Büyük Anne 23 cm Vinil El Kuklası",
                "detail" : {
                    "revisedBy" : "CTA",
                    "revisionDate" : ISODate("2016-08-18T08:04:52.448Z")
                }
            }

you have to use something like this:

db.products.find({"fields.UrunAdi.value":"Büyük Anne 23 cm Vinil El Kuklası",
                  "fields.UrunAdi.detail.revisedBy":"CTA",
                  "fields.UrunAdi.detail.revisionDate":
                          {"$date": "2016-08-18T08:04:52.448Z"}
})

If you get some error extracting your date you can try this too:

 db.products.find({"fields.UrunAdi.value":"Büyük Anne 23 cm Vinil El Kuklası",
                   "fields.UrunAdi.detail.revisedBy":"CTA",
                   "fields.UrunAdi.detail.revisionDate":
                           {"$date": {"$gte": new Date("2016-08-18T08:04:52.448Z")}
    })

Upvotes: 2

Related Questions