小编Ash*_*shh的帖子

在mongodb中使用多个条件更新数组

我有这个文件:

{
    "_id" : ObjectId("5b673f525ef92ec6ef16504e"),
    "events" : [ 
        {
            "name" : "Winner",
            "map" : 0,
            "something" : []
        }, 
        {
            "name" : "Winner",
            "map" : 2,
            "something" : []
        }, 
        {
            "name" : "DifferentName",
            "map" : 2,
            "something" : []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

如果我运行以下更新:

db.getCollection('test').updateOne({
    "_id": ObjectId("5b673f525ef92ec6ef16504e"),
    "events.name": "Winner",
    "events.map": 2
},
{$push: {
        "events.$.something": {
                something: "test",
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

我得到了不好的结果:

{
    "_id" : ObjectId("5b673f525ef92ec6ef16504e"),
    "events" : [ 
        {
            "name" : "Winner",
            "map" : 0,
            "something" : [ 
                { …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js mongodb-query

4
推荐指数
1
解决办法
2895
查看次数

MongoError:除了带有explain参数的聚合外,'cursor'选项是必需的

我使用最新版本的mongodb 3.6即使在收到以下错误后......

MongoError: The 'cursor' option is required, except for aggregate with the explain argument
Run Code Online (Sandbox Code Playgroud)
const user = await User.aggregate([
      { $group: {
          _id: '$_id',
          domain: { $push: "$domain" },
          domain: { $push: "$email" },
          domain: { $push: "$userName" }
        }
      }
    ])
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongodb-query aggregation-framework

3
推荐指数
1
解决办法
4008
查看次数

查找 MongoDB 中数组内字段的总和

我有一个数据如下:

> db.PQRCorp.find().pretty()
{
    "_id" : 0,
    "name" : "Ancy",
    "results" : [
            {
                    "evaluation" : "term1",
                    "score" : 1.463179736705023
            },
            {
                    "evaluation" : "term2",
                    "score" : 11.78273309957772
            },
            {
                    "evaluation" : "term3",
                    "score" : 6.676176060654615
            }
    ]
}
{
    "_id" : 1,
    "name" : "Mark",
    "results" : [
            {
                    "evaluation" : "term1",
                    "score" : 5.89772766299929
            },
            {
                    "evaluation" : "term2",
                    "score" : 12.7726680028769
            },
            {
                    "evaluation" : "term3",
                    "score" : 2.78092882672992
            }
    ]
}
{
    "_id" : 2,
    "name" …
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
4550
查看次数

MongoDB:如何在聚合上应用多个 $project

下面给出的是来自 my_collection

{
  "_id":ObjectId("5b3f4e1013aad111501b34c4")
  "Text":["kjsdf sdfjgnks vk ekl sdsdd."],
  "Title":["lkdjf wufk"],
  "List":["3412","1244","7654","8476"],
  "__v":0
}
Run Code Online (Sandbox Code Playgroud)

我想聚合my_collection这样的:

  1. 生成一个字段“totalList”,它包含字段“List”中数组中的项目数。
  2. 防止字段“__v”出现在结果中。
  3. 将输出字段“列表”的值更改为数组的连接
  4. 删除“列表”中字符串末尾的尾随逗号
  5. 将第 4 点生成的字符串剪短,只包含“列表”字段中的前 50 个字符

所以我写了这段代码:

  db.my_collection.aggregate( 
      [ 
         {
           $addFields: {
             totalList: { $size: "$List" }  // I can achieve the 1st point with this
           }
         },
         { $project : { 
                          __v:0,  // I can achieve the 2nd point with this
                          List:
                          {
                            $reduce:  // I can achieve the 3rd point with this
                               {
                                 input: "$List",
                                 initialValue: "",
                                 in: …
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
7473
查看次数

比较同一文档的两个字段

我正在尝试查询一个巨大的 mongo 集合,其中包含大约 50 + 百万条记录。在 mongo 查询中,我只需要几个字段。文档中存在的对象 ID 和 MD5。为此,我做到了

询问 :

db.getCollection('experimental_engine').find({},{"md5":1,"_id":1}) 
Run Code Online (Sandbox Code Playgroud)

结果 :

/* 1 */
{
    "_id" : "5cee41f2ca4e0ebf567ffd1be5cdaf1f",
    "md5" : "1d813cb29082b13efe572e8088f006dd"
}

/* 2 */
{
    "_id" : "fcd79aac0d5c5ebdfd0fa389368ab6f3",
    "md5" : "13a1a6cd5c8f1c5eaf3d409f4d809889"
}


/* 3 */
{
    "_id" : "2a0b42d01892bd9b7368d045a4c7862c",
    "md5" : "2a0b42d01892bd9b7368d045a4c7862c"
}


................
Run Code Online (Sandbox Code Playgroud)

现在,我想同时匹配 "_id" 和 "md5" 并且只得到匹配的值 ( _id = md5) 。

mongo 命令是否支持两个键的匹配值?

请问有什么建议吗?

mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
7275
查看次数

更新深度嵌套的数组 mongodb

我想使用 mongodb 在附件数组中推送一个值。我想使用以下条件更新查询。

_id:ObjectId("5b56bd2f3e18580edc85af73") "cardID": ObjectId("5b56c895d0a04836f71aa776") "commentId":"2"

我想推动附件中的价值,任何帮助将不胜感激

这是一个集合对象:

{
            "_id" : ObjectId("5b56bd2f3e18580edc85af73"),
            "orgId" : "90",
            "createdBy" : "test",
            "name" : "testname",
            "Cards" : [ 
                {
                    "cardID" : ObjectId("5b56c895d0a04836f71aa776"),
                    "cardName" : "test Name",
                    "cardCreated" : "",
                    "reviewer" : "",
                    "priority" : "",
                    "cardPosition" : "",
                    "membersAssigned" : [ 
                        "ggg", 
                        "fff"
                    ],
                    "labels" : [ 
                        "l1", 
                        "l2"
                    ],
                    "description" : "",
                    "attachements" : [],
                    "comments" : [ 
                        {
                            "commentId" : "2",
                            "commentedBy" : "test",
                            "date" : "",
                            "comment" : "Hello world", …
Run Code Online (Sandbox Code Playgroud)

arrays mongodb

3
推荐指数
1
解决办法
614
查看次数

计算 mongodb 中对象内的键数量

我想知道一场演出中预订了多少个座位。

每个节目文档中都有一个“showTakenSeats”对象。

这是初始数据:

    [
    {
        "_id": "5b658d37692f2e3c881960cb",
        "_MovieId": "5b5835dd86ab401494263861",
        "_TheaterId": "5b3dfeb217bc8c23f0e7ec3f",
        "dateTime": "2018-08-04T14:00:00.000Z",
        "showTakenSeats": {
            "0-0": "5b658f79dd8bb938289e2e0b",
            "0-1": "5b658f79dd8bb938289e2e0b",
            "0-2": "5b658f79dd8bb938289e2e0b"
        },
        "__v": 0,
    },
    {
        "_id": "5b658d4a692f2e3c881960cc",
        "_MovieId": "5b584eb186ab401494263868",
        "_TheaterId": "5b45d269a53b0c05f8959260",
        "dateTime": "2018-08-04T13:00:00.000Z",
        "showTakenSeats": {
            "0-0": "5b65904bdd8bb938289e2e0d",
            "0-1": "5b65904bdd8bb938289e2e0d",
            "0-2": "5b65904bdd8bb938289e2e0d"
        },
        "__v": 0,
    },
    {
        "_id": "5b658fd1dd8bb938289e2e0c",
        "_MovieId": "5b575452478afb72ec65f8f6",
        "_TheaterId": "5b45d219a53b0c05f895925f",
        "dateTime": "2018-08-04T20:00:00.000Z",
        "showTakenSeats": {},
        "__v": 0,
    },
    {
        "_id": "5b6593d80d48d9394877dd09",
        "_MovieId": "5b584eb186ab401494263868",
        "_TheaterId": "5b446a8768db4e2e980723ab",
        "dateTime": "2018-08-05T10:00:00.000Z",
        "showTakenSeats": {},
        "__v": 0,
    },
    {
        "_id": "5b65943b0d48d9394877dd0a",
        "_MovieId": "5b584eb186ab401494263868",
        "_TheaterId": …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js aggregation-framework

3
推荐指数
1
解决办法
1960
查看次数

MongoDB $lookup 对一个文档的对象数组进行查找

我在网上搜索过,但找不到与我的情况相符的内容。情况是这样的。

我正在使用聚合将一个集合和来自另一个集合的一个文档组合在一起

restaurants.aggregate([
  {
    $match: {
      _id: {
        $in: idList
      }
    }
  },
  {
    $lookup: {
      from: "tags",
      localField: "details.restaurantType",
      foreignField: "details.restaurantType._id",
      as: "types"
    }
  },
  {
    $project: {
      restaurantName: "$details.restaurantName",
      restaurantType: "$details.restaurantType",
      type: {
        $filter: {
          input: "$types",
          as: "type",
          cond: {
            $eq: ["$$type._id", "$details.restaurantType"]
          }
        }
      },
      currency: "$details.currency",
      costPerPax: "$details.costPerPax"
    }
  }
]);
Run Code Online (Sandbox Code Playgroud)

当前结果

我当前结果中的“类型”字段是 [],我需要一个匹配的值

[
    {
        "id": "5c20c7a0036dda80a8baabcc",
        "restaurantName": "Villagio Restaurant Sutera Mall",
        "type": [],
        "currency": "RM",
        "costPerPax": 22,
    },
    {
        "id": "5c20ceb07715216d3c217b7a",
        "restaurantName": "Thai …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js mongodb-query aggregation-framework

3
推荐指数
1
解决办法
4565
查看次数

MongoDB:检查对象的数组,如果存在则返回true,否则返回false

我正在尝试查询集合中的数组,如果对象存在于数组中,则投影值“true”。如果对象不存在于数组中,则投影“false”。我在 MongoDB 中工作,我对它不太熟悉。

在我的场景中,我有两个正在使用的集合。我正在聚合一组“员工”成员,并且正在对一组“企业”执行 $lookup 功能。在“业务”中,我拥有一系列业务能力。

例如,我有员工集合

staff = [
  ...
  {_id: 1, businessId: 11},
  {_id: 2, businessId: 22},
  ....
]
Run Code Online (Sandbox Code Playgroud)

企业集合

businesses = [
  ...
  {_id: 11, capabilities: ["claiming", "pushing"]},
  {_id: 22, capabilities: ["claiming", "popping"]},
  ....
]
Run Code Online (Sandbox Code Playgroud)

并有一个 $lookup 设置,如

db.getCollection('staff').aggregate([
    {
        $lookup:
            {
                from: "businesses",
                localField: "businessId",
                foreignField: "_id",
                as: "business_Info"
            }
    },
Run Code Online (Sandbox Code Playgroud)

如果“声称”出现在“能力”中,我如何将每个员工的项目 $project 变成 $canClaim: true 之类的值?

arrays mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
2022
查看次数

如何找到数组的长度mongodb

我的数据库具有这种类型的结构。我想查找“ JAN”数组包含的文档数。我正在尝试以不同的方式进行操作,但是没有任何效果。如果有人帮助的话,那会很棒。

 {
            "_id" : ObjectId("5cd8609db20663a19e2c362c"),
            "2017" : {
                "JAN" : [
                    {
                        "SYMBOL" : "20MICRONS",
                        "SERIES" : "EQ",
                        "OPEN" : "33.4",
                        "HIGH" : "34.3",
                        "LOW" : "33",
                    },
                    {
                        "SYMBOL" : "3IINFOTECH",
                        "SERIES" : "EQ",
                        "OPEN" : "5.8",
                        "HIGH" : "5.8",
                        "LOW" : "5.55",
                    },
                    {
                        "SYMBOL" : "3MINDIA",
                        "SERIES" : "EQ",
                        "OPEN" : "11199.9",
                        "HIGH" : "11233",
                        "LOW" : "10861",
                    }
                ]
            }
        }
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
85
查看次数