mongoose 中的 finOne() 失败并显示 MongoServerError: Expected field collationto be of type object

问题描述 投票:0回答:3

我正在尝试使用express和MongoDb实现简单的注册验证,但下面的代码行总是失败并出现以下错误

const emailExist = await User.findOne({email: req.body.email});

错误

_\node_modules\mongodb\lib\cmap\connection.js:203
                callback(new error_1.MongoServerError(document))
                         ^

MongoServerError: Expected field collationto be of type object
at Connection.onMessage (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\connection.js:203:30)
at MessageStream.<anonymous> (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\connection.js:63:60)
at MessageStream.emit (node:events:394:28)
at processIncomingData (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at TLSSocket.ondata (node:internal/streams/readable:754:22)
at TLSSocket.emit (node:events:394:28) {


ok: 0,
code: 14,
codeName: 'TypeMismatch',
'$clusterTime': {
 clusterTime: Timestamp { low: 25, high: 1650230278, unsigned: true },
signature: {
  hash: Binary {
    sub_type: 0,
    buffer: Buffer(20) [Uint8Array] [
       74, 137, 251, 211, 139, 236,
       64,  99,  37,  21,  24, 232,
      160,  41,  22, 158,  46,  34,
       97, 169
    ],
    position: 20
  },
  keyId: Long { low: 2, high: 1641040568, unsigned: false }
}
},
operationTime: Timestamp { low: 25, high: 1650230278, unsigned: true },
[Symbol(errorLabels)]: Set(0) {}
}

用户模型结构

const mongoose = require ('mongoose')
const userSchema = new mongoose.Schema({

firstName: {type: String, required:true},
lastName: {type: String, required:true},
date: {type: Date},
id: {type: String, required:true, unique: true},
idType: {type: String, required:true},
gender: {type: String, required:true},
mobile: {type: String, required:true, unique: true},
email: {type: String, required:true},
password: {type: String, required:true, max: 1024, min: 6}
},
{ collation: 'user-data'})

const model = mongoose.model ('User', userSchema)

module.exports = model

Express服务器代码

router.post('/register', async (req,res)=>{

//validate data before insert
const { error } = registerValidation(req.body);
if(error) {
    return res.status(400).send(error.details[0].message)  
}

//Checking if the user is already int the database
const emailExist = await User.findOne({email: req.body.email});
if(emailExist) {
    return res.status(400).send('Email already exists')
}

//Create new user
const user= new User(req.body);
try {

    const savedUser = await user.save();
    res.send(savedUser)
} catch (error) {
    res.status(400).send(err);
}
})

module.exports = router;
javascript node.js mongodb mongoose
3个回答
1
投票

正如错误所述,它正在寻找

collation
作为一个对象,你将它作为一个字符串。我不确定您在排序规则中使用
user-data
字符串有何用途。根据文档

排序规则允许用户指定特定于语言的字符串比较规则,例如字母大小写和重音符号的规则。

因此,为了使用它,

locale
字段是强制性的。如果您确定想要对此模式进行排序规则,最简单的方法是将您的模式更改为:

const userSchema = new mongoose.Schema({
  firstName: {type: String, required:true},
  lastName: {type: String, required:true},
  date: {type: Date},
  id: {type: String, required:true, unique: true},
  idType: {type: String, required:true},
  gender: {type: String, required:true},
  mobile: {type: String, required:true, unique: true},
  email: {type: String, required:true},
  password: {type: String, required:true, max: 1024, min: 6}
},
{ collation: { locale: 'en_US', strength: 1 } })

0
投票

这是不同设备中猫鼬版本的问题,我通过替换下面的 $ 解决了这个问题

"refreshes.$.refreshAt": SortType.DESC,

到0

"refreshes.0.refreshAt": SortType.DESC,

我希望这对你有用。


0
投票

const userSchema = new mongoose.Schema({
    firstName: { type: String},
    lastName: { type: String },
    email: { type: String, required: [true, "Email is required"],validate:validateEmail},
    role: { type: String, default: "user" },
    status: { type: Boolean, default: true },
    createdAt: {type: Date, default: Date.now()}
}, {
    collation: 'users'
})

const userSchema = new mongoose.Schema({
    firstName: { type: String},
    lastName: { type: String },
    email: { type: String, required: [true, "Email is required"],validate:validateEmail},
    role: { type: String, default: "user" },
    status: { type: Boolean, default: true },
    createdAt: {type: Date, default: Date.now()}
}, {
    collaction: 'users'
})

“集合拼写错误 您必须将排序规则更改为集合”

© www.soinside.com 2019 - 2024. All rights reserved.