I am getting a MongoDB error when trying to insert a document without nickname second time. The document already have unique field and non required.
Here is my Mongoose model:
var schema = new Schema ({
username: {
type: String,
required: false
},
nickname: {
type: String,
required: false,
unique: true,
trim: true
},
status: {
type: String,
required: false,
trim: true,
minlength: 1,
maxlength: 100
},
avatar: String,
online: {
type: Boolean,
default: false
},
created: {
type: Date,
default: Date.now
},
device: {
type: ObjectId,
ref: 'Device',
required: false
},
createdRooms:[{
type: Schema.Types.ObjectId,
ref: 'Room'
}],
facebook: {
facebookToken: {
type: String,
required: false,
trim: true,
unique: false
},
facebookId: {
type: String,
required: false,
trim: true,
unique: false
}
}
},
{
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
});
For the first time, the document without a nickname is added to the database, but when I want to save another document without a nickname, I get an error:
MongoError: E11000 duplicate key error collection: grooptag.users index: nickname_1 dup key: { : null }
So I looked up "MongoDB not required but unique" and I found this: mongoDB/mongoose: unique if not null
It seems you need to use sparse: true instead of required: false to get what you want.
Edit: Reading up the MongoDB documentation on sparse indexes, however, I was redirected to partial indexes which seem to be the way to go from MongoDB 3.2 onward: https://docs.mongodb.com/manual/core/index-partial/#index-type-partial
The problem is that while the documentation clearly states:
Partial indexes represent a superset of the functionality offered by sparse indexes and should be preferred over sparse indexes.
It does not seem to be true for sparse and unique indexes, since they also state on the same page:
A partial index with a unique constraint does not prevent the insertion of documents that do not meet the unique constraint if the documents do not meet the filter criteria.
Way to contradict themselves... :/
Related
I am getting the following error when trying to create a document in a collection.
MongoServerError: E11000 duplicate key error collection: stock-trading-system-db.trades index: user_id_1 dup key: { user_id: ObjectId('6266de71b90b594dab9037f3') }
Here is my schema:
const tradeSchema = new mongoose.Schema({
user_id: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
stock_id: {
type: Schema.Types.ObjectId,
ref: 'Stock',
required: true
},
stock_amount: {
type: Number,
required: true
},
stock_value: {
type: Number,
required: true
},
time_of_trade: {
type: Date,
required: true,
default: new Date()
},
trade_status: {
type: String,
enum: ['Approved', 'Declined', 'Pending'],
required: true,
default: 'Pending'
}
}, { collection: 'trades' })
I don't want user_id and stock_id to be unique, i just want it to check that those ObjectIDs exist in their respective collections before making the trade document. How do i achieve this?
It looks like a collection saves the schema that is defined in mongoose, and even if you change the schema, the unique values will stay the same inside the collection.
So even though i had removed
unique: true;
from my mongoose schema, it hadn't removed this from the collection in my DB.
Therefore the solution to this is to delete the collection and recreate it.
here is my course schema
const CourseSchema = new Schema(
{
courseName: {
type: String,
required: true,
lowercase: true,
},
comments: [
[
{
user: {
type: Schema.Types.ObjectId,
ref: "Users",
required: true,
},
comment: {
type: String,
required: true,
},
createdAt: {
type: Date,
required: true,
},
},
],
],
},
{
timestamps: true,
}
);
const Course = mongoose.model("Course", CourseSchema);
I want to populate the user field. I've tried many stack overflow solutions but none of them works for me.
I populated the model like this but, doing so it only populates the first index the of every model.
courses = await Course.findOne({}).populate({
path: "comments.0.0.user",
});
You can populate another level deeper, here's what you need to do:
db.Course.findOne({}).populate({"path":"comments",populate:[{
path:"user",
model:"Users"
}]})
Another way of nested populating data:
db.Course.findOne({}).populate("comments.user")
I have a mongoose schema field called "bookmarks" and I would like it to reference more than one model. for example, if I have 2 other models named "post" and "complain". I would like the user to be able to bookmark both of them.
const userSchema = new mongoose.Schema({
fullname: {
type: String,
required: true,
trim: true,
lowercase: true
},
username: {
type: String,
unique: true,
required: true,
trim: true,
lowercase: true
},
email: {
type: String,
unique: true,
required: true,
trim: true,
lowercase: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Email is invalid')
}
}
},
bookmarks: [{
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'Post'
}],
})
below is a post model, where users can post things in general
const postSchema = new mongoose.Schema({
body: {
type: String,
required: true,
trim: true,
lowercase: true
}
})
below is a complain model, where users can post a complain
const complainSchema = new mongoose.Schema({
body: {
type: String,
required: true,
trim: true,
lowercase: true
}
})
How can I get the bookmarks field in the user model to be able to get the object id of both the complain model and the post model?
You can nest those two models in userSchema itself just like you are doing with bookmark schema .
You can also refer to this link , i hope it will resolve your query.
Link - : What is the proper pattern for nested schemas in Mongoose/MongoDB?
Here is the correct way to achieve this.
First remove bookmarks
Add this
ref: {
kind: String, // <-- Model Name post,complain
item: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'ref.kind',
fields: String,
},
},
When you want to fetch the records,you can use populate
model.User.find({})
.populate({ path: 'ref.item' })
.then(data=>{console.log(data)})
.catch(err => {console.log(err)});
I have a user which should have the following fields and currently has following schema:
const UserSchema = new Schema(
{
email: {
type: String,
required: true,
index: { unique: true },
lowercase: true,
},
isVerified: { type: Boolean, default: false }, // whether user has confirmed his email
name: { type: String, required: false },
password: { type: String, required: true, minLength: 6 }, // object with next two included?
passwordResetExpires: Date,
passwordResetToken: String,
roles: [{ type: 'String' }], // Array of strings?
username: { type: String, required: false },
token: [{ type: String, required: false }], // used to send verification token via email
},
{ timestamps: true },
);
So yes, what is the world's default standard for organising user schemas. This schema's fields are pretty common, right?
I created a model as follow:
in the model "name", "commodityID", "totalAmount" are required, but notice commodity ID and totalAmount is part of an inner object "productDetails", and now I am using express-validator to do server-side validation as like this
this validation works for "name" fields which make sense but it doesn't work for the "totalAmount" and "commodityID" which are fields of an inner object,
and it is the pics I took throw postman
may you guys show me the right way to solve this problem
for array i.e
productDetails: [
{
commodityID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "commodity",
},
perOnePrice: { type: String, required: true },
totalAmount: { type: Number, required: true },
},
],
use
[
body('productDetails.*.commodityID').not().isEmpty()
body('productDetails.*.perOnePrice').not().isEmpty()
body('productDetails.*.totalAmount').not().isEmpty()
]
For nested object, lets suppose:
productDetails: {
commodityID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "commodity",
},
perOnePrice: { type: String, required: true },
totalAmount: { type: Number, required: true },
},
use
[
body('productDetails.commodityID').not().isEmpty()
body('productDetails.perOnePrice').not().isEmpty()
body('productDetails.totalAmount').not().isEmpty()
]
Use Wildcard * for nested Objects Read
Here
[
check('name', " ").not().isEmpty()
check('productDetails.commdityID', " ").not().isEmpty()
check('productDetails.totalAmount', " ").not().isEmpty()
]