如何在mongo DB

时间:2019-11-05 01:38:07

标签: node.js mongodb mongoose

我正在尝试更新Mongo DB中的值。但我不知道该怎么办?

我的收藏

[
  {
    "_id": ObjectId("5dc0ca61299ca457b954ebe9"),
    "Emp No": "a1yyy4n9",
    "Emp Name": "anuj sharma",
    "Card No": "27536",
    "Department": "accenture",
    "Gender": "",
    "Att Date": ISODate("2019-09-06T18:30:00Z"),
    "In Time": ISODate("2019-09-07T04:10:00Z"),
    "Out Time": ISODate("2019-09-07T11:42:00Z"),
    "Status": "O",
    "UpdatedStatus": "L",
    "Late By": "",
    "Early By": "",
    "Total Hour": "07:32",
    "OT Hour": "07:32",
    "Location": "plot 5",
    "TmResource": "Y",
    "ReportingTo": "b0207296",
    "PartnerManagerSubmitted": true,
    "ManagerSubmitted": false,
    "attendanceAdded": false,
    "ManagerId": "",
    "Remarks": "",
    "id": "a1yyy4n9-Sat Sep 07 2019 00:00:00 GMT+0530-plot 5",
    "__v": 0
  }{
    "_id": ObjectId("5dc0ca61299ca457b954ebf1"),
    "Emp No": "a1yyy4n9",
    "Emp Name": "anuj sharma",
    "Card No": "27536",
    "Department": "accenture",
    "Gender": "",
    "Att Date": ISODate("2019-09-20T18:30:00Z"),
    "In Time": ISODate("2019-09-21T03:58:00Z"),
    "Out Time": ISODate("2019-09-21T11:40:00Z"),
    "Status": "O",
    "UpdatedStatus": "L",
    "Late By": "",
    "Early By": "",
    "Total Hour": "07:42",
    "OT Hour": "07:42",
    "Location": "plot 5",
    "TmResource": "Y",
    "ReportingTo": "b0207296",
    "PartnerManagerSubmitted": true,
    "ManagerSubmitted": false,
    "attendanceAdded": false,
    "ManagerId": "",
    "Remarks": "",
    "id": "a1yyy4n9-Sat Sep 21 2019 00:00:00 GMT+0530-plot 5",
    "__v": 0
  }{
    "_id": ObjectId("5dc0ca61299ca457b954ec25"),
    "Emp No": "a1z908kw",
    "Emp Name": "pratishtha rai",
    "Card No": "35151",
    "Department": "accenture",
    "Gender": "",
    "Att Date": ISODate("2019-09-06T18:30:00Z"),
    "In Time": ISODate("2019-09-07T03:43:00Z"),
    "Out Time": ISODate("2019-09-07T12:47:00Z"),
    "Status": "O",
    "UpdatedStatus": "L",
    "Late By": "",
    "Early By": "",
    "Total Hour": "09:04",
    "OT Hour": "09:04",
    "Location": "plot 5",
    "TmResource": "Y",
    "ReportingTo": "b0207296",
    "PartnerManagerSubmitted": true,
    "ManagerSubmitted": false,
    "attendanceAdded": false,
    "ManagerId": "",
    "Remarks": "",
    "id": "a1z908kw-Sat Sep 07 2019 00:00:00 GMT+0530-plot 5",
    "__v": 0
  }{
    "_id": ObjectId("5dc0ca61299ca457b954ec29"),
    "Emp No": "a1z908kw",
    "Emp Name": "pratishtha rai",
    "Card No": "35151",
    "Department": "accenture",
    "Gender": "",
    "Att Date": ISODate("2019-09-20T18:30:00Z"),
    "In Time": ISODate("2019-09-21T03:41:00Z"),
    "Out Time": ISODate("2019-09-21T08:59:00Z"),
    "Status": "O",
    "UpdatedStatus": "L",
    "Late By": "",
    "Early By": "",
    "Total Hour": "05:18",
    "OT Hour": "05:18",
    "Location": "plot 5",
    "TmResource": "Y",
    "ReportingTo": "b0207296",
    "PartnerManagerSubmitted": true,
    "ManagerSubmitted": false,
    "attendanceAdded": false,
    "ManagerId": "",
    "Remarks": "",
    "id": "a1z908kw-Sat Sep 21 2019 00:00:00 GMT+0530-plot 5",
    "__v": 0
  }
]

我只想首先找到所有nodes并将ManagerSubmitted设置为true并更新statusUpdatedStatus中出现的任何值

期望的输出结果

  [
    {
        "_id": ObjectId("5dc0ca61299ca457b954ebe9"),
        "Emp No": "a1yyy4n9",
        "Emp Name": "anuj sharma",
        "Card No": "27536",
        "Department": "accenture",
        "Gender": "",
        "Att Date": ISODate("2019-09-06T18:30:00Z"),
        "In Time": ISODate("2019-09-07T04:10:00Z"),
        "Out Time": ISODate("2019-09-07T11:42:00Z"),
        "Status": "L",
        "UpdatedStatus": "L",
        "Late By": "",
        "Early By": "",
        "Total Hour": "07:32",
        "OT Hour": "07:32",
        "Location": "plot 5",
        "TmResource": "Y",
        "ReportingTo": "b0207296",
        "PartnerManagerSubmitted": true,
        "ManagerSubmitted": true,
        "attendanceAdded": false,
        "ManagerId": "",
        "Remarks": "",
        "id": "a1yyy4n9-Sat Sep 07 2019 00:00:00 GMT+0530-plot 5",
        "__v": 0
    }{
    "_id": ObjectId("5dc0ca61299ca457b954ebf1"),
        "Emp No": "a1yyy4n9",
        "Emp Name": "anuj sharma",
        "Card No": "27536",
        "Department": "accenture",
        "Gender": "",
        "Att Date": ISODate("2019-09-20T18:30:00Z"),
        "In Time": ISODate("2019-09-21T03:58:00Z"),
        "Out Time": ISODate("2019-09-21T11:40:00Z"),
        "Status": "L",
        "UpdatedStatus": "L",
        "Late By": "",
        "Early By": "",
        "Total Hour": "07:42",
        "OT Hour": "07:42",
        "Location": "plot 5",
        "TmResource": "Y",
        "ReportingTo": "b0207296",
        "PartnerManagerSubmitted": true,
        "ManagerSubmitted": true,
        "attendanceAdded": false,
        "ManagerId": "",
        "Remarks": "",
        "id": "a1yyy4n9-Sat Sep 21 2019 00:00:00 GMT+0530-plot 5",
        "__v": 0
}{
    "_id": ObjectId("5dc0ca61299ca457b954ec25"),
        "Emp No": "a1z908kw",
        "Emp Name": "pratishtha rai",
        "Card No": "35151",
        "Department": "accenture",
        "Gender": "",
        "Att Date": ISODate("2019-09-06T18:30:00Z"),
        "In Time": ISODate("2019-09-07T03:43:00Z"),
        "Out Time": ISODate("2019-09-07T12:47:00Z"),
        "Status": "L",
        "UpdatedStatus": "L",
        "Late By": "",
        "Early By": "",
        "Total Hour": "09:04",
        "OT Hour": "09:04",
        "Location": "plot 5",
        "TmResource": "Y",
        "ReportingTo": "b0207296",
        "PartnerManagerSubmitted": true,
        "ManagerSubmitted": true,
        "attendanceAdded": false,
        "ManagerId": "",
        "Remarks": "",
        "id": "a1z908kw-Sat Sep 07 2019 00:00:00 GMT+0530-plot 5",
        "__v": 0
}{
    "_id": ObjectId("5dc0ca61299ca457b954ec29"),
        "Emp No": "a1z908kw",
        "Emp Name": "pratishtha rai",
        "Card No": "35151",
        "Department": "accenture",
        "Gender": "",
        "Att Date": ISODate("2019-09-20T18:30:00Z"),
        "In Time": ISODate("2019-09-21T03:41:00Z"),
        "Out Time": ISODate("2019-09-21T08:59:00Z"),
        "Status": "L",
        "UpdatedStatus": "L",
        "Late By": "",
        "Early By": "",
        "Total Hour": "05:18",
        "OT Hour": "05:18",
        "Location": "plot 5",
        "TmResource": "Y",
        "ReportingTo": "b0207296",
        "PartnerManagerSubmitted": true,
        "ManagerSubmitted": true,
        "attendanceAdded": false,
        "ManagerId": "",
        "Remarks": "",
        "id": "a1z908kw-Sat Sep 21 2019 00:00:00 GMT+0530-plot 5",
        "__v": 0
}
]

请参见updatedStatusstatus都是相同的。 我可以这样做

db.collection.update(
            {
                $and : [
                    {
                        $expr:{
                            $ne:["$UpdatedStatus", "$Status"]
                        }
                    },
                    {'ReportingTo': 'b0207296' }
                ]
            }
           , {"$set": {"ManagerSubmitted": true}}, {"multi": true});

我不知道如何将updatedstaus的值设置为status的值

任何更新?

1 个答案:

答案 0 :(得分:0)

您可以使用addFields并且mongo版本应该是above or equal to 3.4 对于参考:https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/

您的查询应如下:

db.collectionName.aggregate( [
  {
    $addFields: {
      status : "$updatedstaus",     
    }
  }
] )