• 请不要在回答技术问题时复制粘贴 AI 生成的内容
SlipStupig
V2EX  ›  程序员

Mongodb 如何得到数组的长度?

  •  
  •   SlipStupig · Sep 5, 2017 · 7214 views
    This topic created in 3200 days ago, the information mentioned may be changed or developed.

    我有一个数据库里面大概有 200 万个文档, 数据结构大致如下:

    {
    "history_id":  [1, 2, 3],
    "author": "andy",
    ...
    }
    

    如何得到数组的长度并统计当前集合内history_id这个字段有总共有多少个元素?

    2 replies    2017-09-05 15:01:12 +08:00
    way2explore2
        1
    way2explore2  
       Sep 5, 2017   ❤️ 1
    Please google and see:
    `
    db.test.insert({username:"Alex", tags: ['C#', 'Java', 'C++'] });
    db.test.aggregate(
    {$match: {username : "Alex"}},
    {$unwind: "$tags"},
    {$project: {count:{$add:1}}},
    {$group: {_id: null, number: {$sum: "$count" }}}
    );
    { "result" : [ { "_id" : null, "number" : 3 } ], "ok" : 1 }
    `
    https://stackoverflow.com/questions/6722850/querying-internal-array-size-in-mongodb#13896341
    scriptB0y
        2
    scriptB0y  
       Sep 5, 2017
    如何得到数组的长度并统计当前集合内 history_id 这个字段有总共有多少个元素?

    这句话我没看懂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1037 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 22:53 · PVG 06:53 · LAX 15:53 · JFK 18:53
    ♥ Do have faith in what you're doing.