以下是模型user中的user.js模式 -
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, unique: true },
password: { type: String, require:true },
},
facebook: {
id : { type: String },
token : { type: String },
email : { type: String },
name : { type: String }
}
});
var User = mongoose.model('User',userSchema);
module.exports = User;
Run Code Online (Sandbox Code Playgroud)
这就是我在我的控制器中使用它的方式 -
var user = require('./../models/user.js');
Run Code Online (Sandbox Code Playgroud)
这就是我在db中保存它的方法 -
user({'local.email' : …Run Code Online (Sandbox Code Playgroud) 我使用Mongoose/MongoDb创建唯一索引时遇到问题,无法使其工作.在设置唯一索引时,我可以添加两个具有相同属性值的文档.
我已经尝试了我能想到的一切 - 重新启动(一切)改变语法等.
这是我用来保存实体的方法:
create : function(entity, definition, successFn, errorFn){
var model = mongoose.model(entity);
newModel = new model(definition);
newModel.save(function(error) {
if(error){
if(!errorFn){
throw error;
}
errorFn(newModel);
return;
}
successFn(newModel);
});
}...
Run Code Online (Sandbox Code Playgroud)
<<
var Something = new Schema({
objectId : ObjectId,
name : { type : String, index: { unique: true }},
url : { type : String, index: { unique: true }},
...etc
mongoose.model('Something', Something);
Run Code Online (Sandbox Code Playgroud)
[conn1] insert xxxxx.agencies 1526ms
[conn1] building new index on { name: …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个具有“ facebookid”键的用户架构,这是一个唯一值,仅当用户使用facebook进行注册时才给出,而不是当用户通过google或本地进行注册时才给出。当用户在本地注册时,默认将facebookid设置为null。但是我得到了错误:'insertDocument :: caused by :: 11000 E11000 duplicate key error index: db22.users.$facebookid_1 dup key: { : null }'
这是模式:
let UserSchema = new Schema({
facebookid: {
type: String,
required: false, // only required for facebook users
unique: true,
default: null
},
// more details + keys below ...
})
Run Code Online (Sandbox Code Playgroud)
因此,如果我还希望条目唯一,那么如果值为null,如何允许重复的条目用于facebookfacebookid?
即我不想看到两个类似的String条目:
不好:
* User: {_id: 123, facebookid: '4325',username:'joe'}
* User: {_id: 432, facebookid: '4325', username: 'pat'}
Run Code Online (Sandbox Code Playgroud)
好的:
* User: {_id: 123, facebookid: null,username:'joe'}
* User: {_id: 432, facebookid: …Run Code Online (Sandbox Code Playgroud) 这是 users.service.ts 中的 addUser 方法 -
// post a single user
async addUser(createUserDTO: CreateUserDTO): Promise<User> {
const newUser = await this.userModel(createUserDTO);
return newUser.save();
}
Run Code Online (Sandbox Code Playgroud)
这是 users.controller.ts 中的 POST 控制器方法 -
// add a user
@Post('/create')
async addUser(@Res() res, @Body() createUserDTO: CreateUserDTO) {
const user = await this.usersService.addUser(createUserDTO);
return res.status(HttpStatus.OK).json({
message: "User has been created successfully",
user
})
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 mongo 集合 findOne 方法,并获取用户详细信息(如果它已存在于 mongo 中),但无法理解如何在存在时引发异常以及在不存在时将其以嵌套 js 样式添加到 mongodb 中 -
const user = await this.userModel.findOne({ userName: createUserDTO.userName });
Run Code Online (Sandbox Code Playgroud)
我将不胜感激的帮助:)
例如,如果我有这个架构
var userSchema = mongoose.Schema({
username: String,
email: String,
password: String,
_todo: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Todo'}]
});
Run Code Online (Sandbox Code Playgroud)
我希望用户名是唯一的密钥,其他用户无法复制.我怎样才能做到这一点?
为什么mongoose在这个剧本中根本不起作用
var child_process = require('child_process');
// Load required packages
child_process.exec("mongo test --eval 'db.users.drop();'", function(err){
var mongoose = require('mongoose');
console.log(mongoose.version);
mongoose.connect('mongodb://localhost:27017/test');
// Define our user schema
var json = {};
json.phone = { type: String, required: true, unique: true};
var UserSchema = new mongoose.Schema(json);
var Model = mongoose.model('user', UserSchema);
var jp = new Model({ phone: "123456"});
mongoose.connection.on('open', function(){
console.log(jp);
jp.save(function(err){
console.log(err);
var jp2 = new Model({ phone: "123456"});
console.log(jp2);
jp2.save(function(err){
console.log(err);
process.exit();
});
})
});
});
Run Code Online (Sandbox Code Playgroud)
我很困惑,结果就像
3.8.20
{ phone: '123456', …Run Code Online (Sandbox Code Playgroud) 我的节点应用程序中有一个user.js模型,我希望用户名和其他一些字段唯一。在我的模型文件中,我正确声明了unique如下所示的类型:
// User Schema
const UserSchema = new Schema({
// PERSONAL USER INFO
username: {
type: String,
required: true,
index: true,
unique: true,
},
email: {
type: String,
required: true,
index: true,
unique: true
},
password: {
type: String,
required: true,
},
....
});
Run Code Online (Sandbox Code Playgroud)
但是,在重新启动服务器和mongo会话之后,我仍然可以使用相同的用户名和相同的电子邮件创建用户。我可以从mongo shell和前端用户注册页面上执行此操作。
我还缺少第二部分吗?我现在不确定如何正确执行该unique类型。谢谢!
我假设在字段中添加unique:true将停止使用相同的值保存到数据库。但是即时消息仍然允许这样做。
“猫鼬”:“ ^ 5.4.19”,
const SessionSchema = new Schema({
jobId: {
type: String,
required: false,
unique: true,
index: true,
},
productId: {
type: String,
required: true,
},
status: {
type: String,
default: "Pending",
},
mode: {
type: String,
default: "authentication",
},
result: {
type: Schema.Types.Mixed,
},
requests: [RequestSchema],
callback_at: {
type: Date,
},
}, {
timestamps: { createdAt: "created_at", updatedAt: "updated_at" },
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除并重新创建集合。参见下图,我可以使用相同的jobId创建1的新会话。
public store = async (req: Request, res: Response): Promise<any> => {
const input = req.body;
let …Run Code Online (Sandbox Code Playgroud)