如何在不创建新猫鼬项目的情况下推送我的新项目

How do I push my new item without creating new mongoose item

我想附加或推送我在数组中输入的新项目而不创建新的猫鼬模式..我的意思是我只会推送项目而不创建新的_id...这是我的代码..

对于mongoose.Schema()

const mainSchema = new Schema({
    likes:{
        type:Number,
        max:100000
    },
    people:[{
        key:{type:String},
        name:{type:String}
    }]
},{
    timestamps:true
})

对于 post 我的物品的路线..

router.route('/item/1').post((req,res) => {

    const { likes, people } = req.body


    const FirstItem = Main({
        likes,
        // people
    })

    FirstItem.people.push(people)

    FirstItem.save()
        .then(likes => res.json('New User Added'))
        .catch(err => res.status(400).json('Error :' + err))

})  

如您所见,我没有在我的 post 中输入 new Main({}),因为我不想创建一个新的 _id..但我想将我的项目推送到我的每当我创建另一个新项目时数组....这就是我在 postman..

中的写法
{
    "likes":0,
    "people":[
        {
            "key":"Tes22t",
            "name":"Tit213an"
        }
    ]
}

现在,如果我在我的 POST 方法中 post 更改了一些内容。像 "key":"testetsee","name":"123123123" 它会给我这样的错误... "Error :ValidationError: people.0._id: Cast to ObjectId failed for value \"[ { key: 'Tes22t', name: 'Tit213an' } ]\" (type Array) at path \"_id\""

我想我的问题是推送项目?还是 post?还是我只需要更新它?

已编辑

我在我的前端有这个来将它传递到我的后端,因为我希望我的每个项目都收到其帐户已注册以便它可以存储用户名和密钥..

axios.post('http://localhost:7171/likes/item/'+1,{ people:{name:name,key:key }})
          axios.post('http://localhost:7171/likes/item/'+2,{ people:{name:name,key:key } })
          axios.post('http://localhost:7171/likes/item/'+3,{ people:{name:name,key:key } })
          axios.post('http://localhost:7171/likes/item/'+4,{ people:{name:name,key:key } })
          axios.post('http://localhost:7171/likes/item/'+5,{ people:{name:name,key:key } })

然后你给我的密码就在这里...

router.route('/item/:id').post((req,res) => {

    const id = req.params.id
    
    console.log(id)

    if (!id) return res.status(400).json({ message: "missing id" });

    const { likes, people } = req.body;

    Main
        .updateOne(
        { _id: id },
        {
            $addToSet: { people: { $each: people } },
            $set: {
                likes,
            },
        },
        )
        .then((likes) => res.json({ message: "New User Added" }))
        .catch((err) => res.status(400).json("Error :" + err));

})  

因为有 5 个项目我将同时 post,那么它应该在我的数据库中有 5 个项目,并且只要我有新用户将它附加到我的数组列表中的每个项目中,就会更新数组对象。

我的 postman 收到的是这个... "Error :CastError: Cast to ObjectId failed for value \"1\" (type string) at path \"_id\" for model \"liker-model\""

如果您只想将一个元素推送到该对象的人员数组中,您应该使用 updateOne() 而不是 save()

所以代码会像

router.route('/item/1').post((req,res) => {

    const { likes, people } = req.body

    const model = mongoose.model('collection_name', mainSchema);

    model.updateOne({like}, {$addToSet: { people: {$each: people} }})
        .then(likes => res.json('New User Added'))
        .catch(err => res.status(400).json('Error :' + err))


})  

在与主题所有者讨论后,我创建了新的答案,其中 /1 作为项目 ID

const _ = require("lodash");

router.route("/item/:id").post((req, res) => {
  const id = _.get(req, "params.id");
  if (!id) return res.status(400).json({ message: "missing id" });
  const { likes, people } = req.body;

  const model = mongoose.model("collection_name", mainSchema);

  model
    .updateOne(
      { _id: id },
      {
        $addToSet: { people: { $each: people } },
        $set: {
          likes,
        },
      },
    )
    .then((likes) => res.json({ message: "New User Added" }))
    .catch((err) => res.status(400).json("Error :" + err));
});