【问题标题】:MongoDB compare current day with day in mongodb and insertOne() if not matchMongoDB 将当前日期与 mongodb 中的日期进行比较,如果不匹配则 insertOne()
【发布时间】:2021-11-24 16:52:26
【问题描述】:

我正在尝试根据 2 个条件更新、创建或替换我的集合中的某些字段。 这是我所拥有的:

[
  {
    _id: ObjectId("5a934e000102030405000000"),
    userId:ObjectId("61965bead6af120015c7e9cc"),
    date: ISODate("2021-11-25T16:04:35.606Z"),
    ip: "100.000.000.0"
  },
  {
    _id: ObjectId("11934e000102030405000111"),
    userId:ObjectId("61965bead6af120015c7e9cd"),
    date: ISODate("2021-11-25T14:08:32.992Z"),
    ip: "200.000.000.0"
  }
]

我的变量是:

userId : req.user._id

日期:新日期()

ip : req.socket.remoteAddress

抱歉,编辑,我重新表述: 我需要的是使用 userId(not unique) 创建新条目 IF 日期与 mongo 中的日期不同,否则什么也不做

不好:

    db.collection.aggregate([
  {
    $project: {
      dateinDB: {
        $dateToString: {
          date: ISODate("2021-11-23T16:04:35.606Z"),
          format: "%Y-%m-%d"
        }
      },
      current: {
        $dateToString: {
          date: new Date(),
          format: "%Y-%m-%d"
        }
      }
    }
  },
  {
    $addFields: {
      date: {
        $cond: {
          if: {
            $eq: [
              "$userId",
              "$req.user._id"
            ]
          },
          then: {
            $cond: {
              if: {
                $ne: [
                  "$dateinDB",
                  "$current"
                ]
              },
              then: {
                date: new Date()
              },
              else: {
                date: "test"
              }
            }
          },
          else: {
            "userId": "$req.user._id",
            "date": new Date(),
            "ip": "$req.socket.remoteAddress"
          }
        }
      }
    }
  }
])

创建:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "current": "2021-11-23",
    "date": {
      "date": "test"
    },
    "dateinDB": "2021-11-23"
  },
  {
    "_id": ObjectId("11934e000102030405000111"),
    "current": "2021-11-23",
    "date": {
      "date": "test"
    },
    "dateinDB": "2021-11-23"
  }
]

这里是游乐场:https://mongoplayground.net/p/4UtuONJiL6Z

我需要:

userID : ObjectId("61965bead6af120015c7e9cc")
date: new Date() //ISODate("2021-11-25T16:08:35.606Z") for example

=> 什么都没有

和:

userID : ObjectId("61965bead6af120015c7e9cc")
date: new Date() //ISODate("2021-11-26T16:08:35.606Z")

结果:

[
      {
        _id: ObjectId("5a934e000102030405000000"),
        userId:ObjectId("61965bead6af120015c7e9cc"),
        date: ISODate("2021-11-25T16:04:35.606Z"),
        ip: "100.000.000.0"
      },
      {
        _id: ObjectId("11934e000102030405000111"),
        userId:ObjectId("61965bead6af120015c7e9cd"),
        date: ISODate("2021-11-25T14:08:32.992Z"),
        ip: "200.000.000.0"
      },
      {
        _id: ObjectId("645465454565465465465465"),
        userId:ObjectId("61965bead6af120015c7e9cc"),
        date: ISODate("2021-11-26T16:08:35.606Z"),
        ip: "100.000.000.0"
      },
    ]

任何帮助将不胜感激!

【问题讨论】:

  • "...AND if date 与 mongo 中的日期不是同一天"是什么意思
  • 并判断userId是否唯一?
  • @TalESid "...如果日期与 mongo 中的日期不同" => YES userId 是唯一的
  • 表示更新date值如果(用户存在并且保存在mongodb中的日期不是今天,即new Date())?
  • 我宁愿建议$dateTrunc({units: 'day'})而不是$dateToString

标签: node.js mongodb mongodb-query aggregation-framework upsert


【解决方案1】:

试试这个:

db.collection.aggregate([
  {
    
      $project: 
      {              
          dateinDB:{$dateToString: {date:"$date",format:"%Y-%m-%d"}},
          current:{$dateToString: {date:new Date(),format:"%Y-%m-%d"}}
      }
  },
   { $addFields: {
      date: {
        $cond: {
          if: { $eq: ["$userId",'$req.user._id'] },
          then: {
            $cond: {
              if:{$ne: [ "$dateinDB" ,"$current"]},
              then :{$date:"$current"}
            }
          },
          else: {
            'userId': '$req.user._id',
            'date': new Date(),
            'ip':'$req.socket.remoteAddress'
          }
        }
      }
    }
   }
  
]);

【讨论】:

  • 谢谢,结束,mongoplayground.net/p/aw3iy2uiK3L => 查询失败:(InvalidPipelineOperator) Invalid $addFields :: 由 :: Unrecognized expression '$date' 引起
  • 对不起,这不是我做的好方法,看看结果:mongoplayground.net/p/4UtuONJiL6Z 结果必须与新条目开头相同(新的_id与另一个相同)或什么都没有。跨度>
猜你喜欢
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多