How to check if Mongo's $addToSet was a duplicate or not

前端 未结 3 2107
南方客
南方客 2021-01-13 04:15

I am using Mongoskin + NodeJS to add new keywords to MongoDB. I want to notify the user that the entry was a duplicate but not sure how to do this.

/*
* POST          


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 04:38

    i am update a array from Collection with this JSON:

    {
        "":""
    }
    

    route.js

    routes.post("/api/:id", Controller.addOne);
    

    Controller.js

    async addOne(req, res) {
        //juryman id to list add
        if (Object.keys(req.body).length === 1) {
          console.log("Size 1");
        }
        await Session.findOneAndUpdate(
          { _id: req.params.id },
          { $addToSet: req.body }
        )
          .then(function(success) {
            res.send("Successfully saved.");
          })
          .catch(function(error) {
            res.status(404).send(error);
          });
      },
    

    I have five arrays in my Collection and this changes the JSON array name-value and updates correctly, the respectively Collection array. This works only for one item.

提交回复
热议问题