(mongodb)如何在嵌入文档中找到空值

时间:2016-10-24 09:38:52

标签: mongodb

我想搜索这一部分:

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

urunadı表示产品名称。

因此,如果productname值为null或为空,我希望将其视为输出。

我尝试了一些,都给出了错误的语法错误。

我试过这个匹配(如果有效,我会为null做)

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

产品是db产品中的集合。

或者失败

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")
            }}}})

这是代码的长版本。我写这个是

首先

use product

然后

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" : [ ]
}

我合并了2个答案,这很有效 produp:PRIMARY> db.products.find({"fields.UrunAdi.value":""}).count() 129855 但它是为所有产品提供的。但我想要每个商人。在sql中,我们可以使用distinct来实现。 db.products.distinct( "merchant" )对此是正确的,但我如何将这两个查询结合起来呢?

2 个答案:

答案 0 :(得分:2)

您的fields字段不是数组,但它只包含一些文档。 如果要查找与此匹配的所有文档:

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

你必须使用这样的东西:

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"}
})

如果您在提取日期时出现错误,也可以尝试:

 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")}
    })

答案 1 :(得分:1)

尝试以下查询:

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

上述查询将返回fields.UrunAdi值为null或fields.UrunAdi不存在或为空对象的记录。

编辑: -

使用如下聚合:

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

有关详细信息,请参阅doc-aggregate

希望这会有所帮助。