简体   繁体   English

如果项目不存在,则将项目添加到数组 mongodb

[英]add item to array if it doesn't exist mongodb

I'm running a simple application with mongoDB + nodejs , I'm trying to achieve the following:我正在使用mongoDB + nodejs运行一个简单的应用程序,我正在尝试实现以下目标:

The unit belongs to a company , the classroom belongs to a unit and the user belongs to a classroom .单位属于公司教室属于单位用户属于教室

In certain moment, I want to add the user to another unit or/and classroom, then he'll belong to 2 or more units/classrooms.在某个时刻,我想将用户添加到另一个单元或/和教室,然后他将属于 2 个或更多单元/教室。

My form will sent only one unit/classroom per time, in this case, I want to add it to the user model unit:[string] and classroom:[string] only if he doesn't previously belong to it.我的表格每次只发送一个单元/教室,在这种情况下,我想将其添加到用户 model unit:[string]classroom:[string] ],前提是他以前不属于它。 So I need to check if the arrays already have the sent data, if don't, add to it.所以我需要检查arrays是否已经有发送的数据,如果没有,添加它。

Mongo has the $addToSet property, and the $ne to do it, but I can't seem to make it work. Mongo 有$addToSet属性和$ne来做这件事,但我似乎无法让它工作。

Here's my code:这是我的代码:

User.findById(req.body._id)
    .select("-__v")
    .exec((err: Error, user: any) => {
            if (err) {
                // display error
            }
            if (!user) {
                // display error
            }
            user.update({
                unit: {
                    $ne: user.unit
                },
                classroom: {
                    $ne: user.classroom
                }
            }, {
                $addToSet: {
                    unit: req.body.unit,
                    classroom: req.body.classroom
                }
            }).exec((err: Error) => {
                if (err) {
                    // Display error
                }

                res.status(200).json({
                    status: "OK",
                    response: response,
                })
                return
            })

在此处输入图像描述

It belongs to "Academy one" and the classroom id reference, I will add him to another unit like "Academy 2" and add another classroom reference, but if I add him to another classroom of "Academy One", I don't want a duplicate item in it's unit array.它属于“Academy one”和classic id reference,我会将他添加到“Academy 2”这样的另一个单元并添加另一个教室引用,但是如果我将他添加到“Academy One”的另一个教室,我不想要其单位数组中的重复项。

When I post the following through postman , it gives me the error:当我通过postman发布以下内容时,它给了我错误:

{
"_id":"5d8ba151248ecb4df8803657", // user id
"unit":"Test", // another unit
"classroom":"5d8a709f44f55e4a785e2c50" // another classroom
}

Response:回复:

{ "status": "NOK", "response": "Cast to [string] failed for value \"[{\"$ne\":[\"Academy One\"]}]\" at path \"unit\"" } { "status": "NOK", "response": "Cast to [string] failed for value \"[{\"$ne\":[\"Academy One\"]}]\" at path \"unit \"" }

What am I missing?我错过了什么?

Actually, I didn't needed the $ne operator, I just needed to use the $addToSet directly实际上,我不需要$ne运算符,我只需要直接使用$addToSet

user.updateOne({
  $addToSet: { unit: req.body.unit, classroom: req.body.classroom }
}).exec((err: Error) => {

Thanks!谢谢!

You need to use $nin instead of $ne , https://docs.mongodb.com/manual/reference/operator/query/nin/您需要使用$nin而不是$nehttps://docs.mongodb.com/manual/reference/operator/query/nin/

unit: {$nin: [user.unit]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM