简体   繁体   English

Mongo数据库查询数组

[英]Mongo DB query arrays

I have a document such as the following: 我有一份如下文件:

{
    "_id" : LUUID("8a831942-1dd6-1942-8759-c6275305d10d"),
    "Record" : "SEK_2Y_9Y",
    "RawDataDictionary" : [[ 1033, ""], [3355,""], [886,"45.8"],[874,"589"]],
    "TimeStamp" : ISODate("2015-02-10T16:27:14.847Z")
}

The "RawDataDictionary" contains pairs of code and value. “RawDataDictionary”包含成对的代码和值。

I want all the documents that their "RawDataDictionary" contains an item with 874 code and any value that is not null/Empty. 我想要所有文档,他们的“RawDataDictionary”包含一个具有874代码的项目和任何非null / Empty的值。

so [874,""] will not obey it but [874,"something"] will. 所以[874,“”]不会遵守它,但[874,“某事”]会。

Any ideas ? 有任何想法吗 ?

Doron. 多伦。

I don't believe there is a way to express your condition on your document in the MongoDB query language (as of 2.6). 我不相信有一种方法可以用MongoDB查询语言表达你的文档条件(从2.6开始)。 The issue is that there's no way to express the "any [second] value that is not null/empty" condition. 问题是没有办法表达“任何[第二个]非空/空值”条件。

db.test.find({ "RawDataDictionary" : [1033, ""] }) // ok

db.test.find({ "RawDataDictionary" : [874, { "$ne" : "" }) // nope

You can use $elemMatch to perform the query if you modify the structure of the RawDataDictionary field: 如果修改RawDataDictionary字段的结构,则可以使用$elemMatch来执行查询:

{
    "Record" : "SEK_2Y_9Y",
    "RawDataDictionary" : [
        { "code" : 1033, "value" : "" }, 
        { "code" : 3355, "value" : "" },  
        { "code" : 886, "value" : "45.8" }, 
        { "code" : 874, "value" : "589" }
    ],
    "TimeStamp" : ISODate("2015-02-10T16:27:14.847Z")
}

db.test.find({ "RawDataDictionary" : {
    "$elemMatch" : { 
        "code" : 874, 
        "value" : { "$nin" : ["", null] } 
    } 
} })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM