如何计算mongodb中集合中的特定元素

时间:2016-01-01 20:20:00

标签: mongodb mapreduce mongodb-query aggregation-framework

我知道您可以使用

计算集合中有多少元素
 collection.find().count(). 

但是,我想知道如何计算集合中项目内的某个元素。例如,我在Documents内有四个图像,如下所示:

"Documents":{
    "1":"image.png",
    "2":"Test.jpg",
    "3":"Next.png"
  }

我想知道如何计算文件中的所有项目?我尝试了一些东西,但没有一个在起作用。有人可以帮忙吗?

示例数据:

{
  "name": "Oran",
  "username": "Oran.Hammes",
  "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/brandonflatsoda/128.jpg",
  "email": "xxxxxx@hotmail.com",
  "dob": "1953-03-21T17:40:17.020Z",
  "phone": "364-846-1607",
  "address": {
    "street": "Schultz Stream",
    "suite": "Suite 618",
    "city": "North Muriel mouth",
    "zipcode": "06447-1081",
    "geo": {
      "lat": "57.1844",
      "lng": "-56.8890"
    }
  },
  "website": "misty.net",
  "company": {
    "name": "Hettinger, Reilly and Stracke",
    "catchPhrase": "Multi-tiered system-worthy database",
    "bs": "best-of-breed evolve e-markets"
  },
  "Documents": [
    {
        "id": "1",
        "name": "image.png",
    },
    {
        "id": "2",
        "name": "Test.jpg",
    },
    {
         "id": "3",
         "name": "Next.png"
    }
  ]
}

在此comment之后,我更改了文档的结构,它看起来像这样:

{
        "_id" : ObjectId("568703d08981f193cf343698"),
        "name" : "Oran",
        "username" : "Oran.Hammes",
        "email" : "xxxxxxx@hotmail.com",
        "dob" : "1953-03-21T17:40:17.020Z",
        "phone" : "364-846-1607",
        "company" : {
                "name" : "Hettinger, Reilly and Stracke",
                "catchPhrase" : "Multi-tiered system-worthy database",
                "bs" : "best-of-breed evolve e-markets"
        },
        "Documents" : [
                {
                        "id" : "1",
                        "name" : "image.png"
                },
                {
                        "id" : "2",
                        "name" : "Test.jpg"
                },
                {
                        "id" : "3",
                        "name" : "Next.png"
                }
        ]
}

1 个答案:

答案 0 :(得分:1)

一般来说,在文档中使用动态密钥并不是一个好主意。在这种情况下你最好的选择是mapReduce

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="price section-content">
  <label>
    <input id="price" type="text" value="" name="price">
    <select id="currency" name="currency">
      <option value="EUR">€</option>
      <option selected="selected" value="USD">$</option>
    </select>
    <span>Price</span>
  </label>
  <label>
    <input id="exchange" type="radio" value="2" name="price-option">
    <span>Exchange</span>
  </label>
  <label>
    <input id="donate" type="radio" value="1" name="price-option">
    <span>Donate</span>
  </label>
</div>

哪个收益率:

var map = function() { 
    emit(this._id, Object.keys(this.Documents).length);
};
var reduce = function(key, values) {};
db.collection.mapReduce(map, reduce, { "out": { "inline": 1 } } )

当然,最好的办法是更改文档结构并使{ "results" : [ { "_id" : ObjectId("5686e2a98981f193cf343697"), "value" : 3 } ], "timeMillis" : 697, "counts" : { "input" : 1, "emit" : 1, "reduce" : 0, "output" : 1 }, "ok" : 1 } 成为一个子文档数组,以便Documents字段值如下所示:

Documents

并使用.aggregate()方法$project您的文档,并返回&#34;文档&#34;中的元素数量。数组使用$size运算符

"Documents": [
    {
        "id": "1",
        "name": "image.png",
    },
    {
        "id": "2",
        "name": "Test.jpg",
    },
    {
         "id": "3",
         "name": "Next.png"
    }
]