Node.js MongoDB使用not并等于

Node.js MongoDB使用not并等于,node.js,mongodb,Node.js,Mongodb,问题 如果手机不存在,则更新手机,否则显示错误手机已存在 与用户在站点上注册时的情况相同。注册后,如果他想更改电子邮件,如果该电子邮件不存在,则更新其电子邮件 希望你们明白我想要什么 资料 代码 使用此代码: let phoneExist = await Agent.findOne({ user: ObjectId('5fb64b808232c9983eab41ce') } }, { _id: ObjectId('5fbcc1fa2afc3e556c32fce0'), phone: '+4479

问题

如果手机不存在,则更新手机,否则显示错误手机已存在

与用户在站点上注册时的情况相同。注册后,如果他想更改电子邮件,如果该电子邮件不存在,则更新其电子邮件

希望你们明白我想要什么

资料

代码

使用此代码:

let phoneExist = await Agent.findOne({ user: ObjectId('5fb64b808232c9983eab41ce') } },
{ _id: ObjectId('5fbcc1fa2afc3e556c32fce0'), phone: '+447911123457'});

console.log(phoneExist)
if(!phoneExist){

const agent = await Agent.findByIdAndUpdate('5fbcc1fa2afc3e556c32fce0', phone, {
new: true,
runValidators: true
});


 res.status(200).json({
success: true,
data: agent
});


}
else{
return next(new ErrorResponse('Phone already exist', 409));
}
使用此代码:

let phoneExist = await Agent.findOne({ user: ObjectId('5fb64b808232c9983eab41ce') } },
{ _id: ObjectId('5fbcc1fa2afc3e556c32fce0'), phone: '+447911123457'});

console.log(phoneExist)
if(!phoneExist){

const agent = await Agent.findByIdAndUpdate('5fbcc1fa2afc3e556c32fce0', phone, {
new: true,
runValidators: true
});


 res.status(200).json({
success: true,
data: agent
});


}
else{
return next(new ErrorResponse('Phone already exist', 409));
}

据我所知,您需要根据特定的id和用户检查提供的电话是否已经存在。如果不存在,更新它,否则抛出错误

在这种情况下,要简单得多:

/* if you are querying on "_id" field, you don't need anything else (i.e. "user") */
const agent = await Agent.findById('5fbcc1fa2afc3e556c32fce0');

/* but if you still want this, then do it this way */
// const agent = await Agent.findOne({_id: '5fbcc1fa2afc3e556c32fce0', user: '5fb64b808232c9983eab41ce'});

if (agent.phone != '+447911123457') {
    agent.phone = '+447911123457';
    await agent.save();
    
    res.status(200).json({
        success: true,
        data: agent
    });
} else
    return next(new ErrorResponse('Phone already exist', 409));
编辑

SQL:

从用户所在的代理中选择*{user}和phone={phone}

MongoDb:

await Agent.findOne({
    "user": { $ne: 'enter user here' }
    "phone": 'enter phone here'
});
就这样

最后编辑 因此,根据@Adam的解决方案,它应该看起来像:

const phoneExist = await Agent.findOne({
    "_id":   { $ne: ObjectId(req.params.id) },
    "user":  ObjectId(req.user.id),
    "phone": data.phone
});

if (! phoneExist) {
    const agent = await Agent.findByIdAndUpdate(req.params.id, {phone: data.phone}, {new: true});
    res.status(200).json({
        success: true,
        data: agent
    });
} else
    return next(new ErrorResponse('Phone already exist', 409));

据我所知,您需要根据特定的id和用户检查提供的电话是否已经存在。如果不存在,更新它,否则抛出错误

在这种情况下,要简单得多:

/* if you are querying on "_id" field, you don't need anything else (i.e. "user") */
const agent = await Agent.findById('5fbcc1fa2afc3e556c32fce0');

/* but if you still want this, then do it this way */
// const agent = await Agent.findOne({_id: '5fbcc1fa2afc3e556c32fce0', user: '5fb64b808232c9983eab41ce'});

if (agent.phone != '+447911123457') {
    agent.phone = '+447911123457';
    await agent.save();
    
    res.status(200).json({
        success: true,
        data: agent
    });
} else
    return next(new ErrorResponse('Phone already exist', 409));
编辑

SQL:

从用户所在的代理中选择*{user}和phone={phone}

MongoDb:

await Agent.findOne({
    "user": { $ne: 'enter user here' }
    "phone": 'enter phone here'
});
就这样

最后编辑 因此,根据@Adam的解决方案,它应该看起来像:

const phoneExist = await Agent.findOne({
    "_id":   { $ne: ObjectId(req.params.id) },
    "user":  ObjectId(req.user.id),
    "phone": data.phone
});

if (! phoneExist) {
    const agent = await Agent.findByIdAndUpdate(req.params.id, {phone: data.phone}, {new: true});
    res.status(200).json({
        success: true,
        data: agent
    });
} else
    return next(new ErrorResponse('Phone already exist', 409));

我已经添加了这个var ObjectId=require'mongodb'。ObjectId;在{$ne:{$ne:'5fbcc1fa2afc3e556c32fcce0'}让phoneExist=wait代理中使用它;仍然存在相同的问题打印什么console.logphoneExist?您需要删除!或$ne。因为您的查询显示电话不存在。我已经添加了这个var ObjectId=require'mongodb'.ObjectId;在{$ne:{$ne:'5fbcc1fa2afc3e556c32fcce0'}让phoneExist=wait代理中使用它;仍然存在相同的问题打印什么console.logphoneExist?您需要删除!或$ne。因为您的查询显示电话不存在。多加注意,你就会得到它。首先,你们完全滥用了美元和。。。然后您的查询并不完全清楚,好像您实际上想要什么结果?在您的代码中,您将用户:{$ne:'5fb64b808232c9983eab41ce'}放入其中,因此您的两个对象都被删除。和phoneExist变为true,因此它进入if块而不是elsefirst。首先,您完全误用了$and。。。然后您的查询并不完全清楚,好像您实际上想要什么结果?在您的代码中,您将用户:{$ne:'5fb64b808232c9983eab41ce'}放入其中,因此您的两个对象都被删除。和phoneExist变为true,因此它进入if块而不是els。在本例中,针对不同的ID插入多个电话,并对其进行尝试。如果您有任何问题,请在此处留下评论“不起作用”。错误:E11000重复键错误收集索引:phone\1此代码工作正常。我自己也测试过。但如果您有问题,请详细说明您的查询。根据您的数据,您需要什么?在这种情况下,多部手机针对不同的ID插入,让我们试一试。如果您有任何问题,请在此处留下评论“不起作用”。错误:E11000重复键错误收集索引:phone\1此代码工作正常。我自己也测试过。但如果您有问题,请详细说明您的查询。根据您的数据,您需要什么?