I'm struggling with Error E11000 duplicate key error collection, I have a schema with another sub-schema array and when I try to insert my schema with the empty array I allways get this error. I tried to set it undefined on pre-save without sucesss... I have deleted my schem from mongoDB and its indexes. The error appears after I insted **autoincrement **in Marker schema.
City Schema:
let mongoose = require('mongoose');
let autoIncrement = require('mongoose-auto-increment');
let Marker = require('./marker');
var MarkerSchema = require('mongoose').model('marker').schema;
//City Schema
//status : 1 Ok, 2 Hidden, 3 Deleted
let citySchema = mongoose.Schema({
id: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
status: {
type: Number,
required: true
},
coordinates: {
latitude: {
type: Number,
required: true
},
longitude: {
type: Number,
required: true
}
},
markers: [MarkerSchema]
});
citySchema.pre('save', function (cb) {
console.log('pre save');
if (this.markers && this.markers.length === 0) {
this.markers = undefined;
}
cb();
});
citySchema.plugin(autoIncrement.plugin, {
model: 'city',
field: 'id',
startAt: 1,
incrementBy: 1
});
let City = module.exports = mongoose.model('city', citySchema);
marker schema
let mongoose = require('mongoose');
let autoIncrement = require('mongoose-auto-increment');
let markerSchema = mongoose.Schema({
status: {
type: Number,
required: true
},
description: {
type: String,
required: true
},
coordinates: {
latitude: {
type: Number,
required: true
},
longitude: {
type: Number,
required: true
}
}
});
markerSchema.plugin(autoIncrement.plugin, {
model: 'marker',
field: 'id',
startAt: 1,
incrementBy: 1
});
let Marker = module.exports = mongoose.model('marker', markerSchema);
Having the same issue ...
Rather sure this is caused by the mongoose-auto-increment module...
try this:
let citySchema = new mongoose.Schema({
id: {type: Number, default: 0, unique: true},
....
I think the error is because you have not defined an initial value where the auto-increment should work and the fact that it is not declared as unique ?
I know this is an old question but for future viewers here is what i did.
I added a type of number on the plugin schema and set unique to false to avoid duplication during manipulation.
so the new plugin schemas will be
markerSchema.plugin(autoIncrement.plugin, {
model: 'marker',
field: 'id',
startAt: 1,
incrementBy: 1,
type: Number,
unique: false
});
and
citySchema.plugin(autoIncrement.plugin, {
model: 'city',
field: 'id',
startAt: 1,
incrementBy: 1,
type: Number,
unique: false
});
Related
I have taken this schema from Here
var mongoose = require('mongoose');
var ContactSchema = module.exports = new mongoose.Schema({
name: {
type: String,
required: true
},
phone: {
type: Number,
required: true,
index: {unique: true}
},
messageCount: {
type: Number,
required: true,
default:0
},
messages: [
{
title: {type: String, required: true},
msg: {type: String, required: true}
}]
}, {
collection: 'contacts',
safe: true
});
I can push message by doing this
let result = await Contact.findByIdAndUpdate(
id,
{$push: {"messages": {title: title, msg: msg}}},
{new : true})
But I want to increase messageCount also in single step where
messageCount can be random also but point is to push and update
I am expecting both message and messageCount to update in single query using any mongoose functions
After Looking to This Post and tried on my doubt
let result = await Contact.findByIdAndUpdate(
id,
{
messageCount:10
$push: {"messages": {title: title, msg: msg}}
},
{
new : true
}
)
I have a "cart" model, and within my "order" model, I would like to have an array that receives the information sent by "cart" stored in an array. How can I do this through ref?
const mongoose = require('mongoose');
const CartSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
note: {
type: String,
required: true,
},
price: {
type: Number,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Cart", CartSchema);
order
const mongoose = require('mongoose');
const OrderSchema = new mongoose.Schema(
{
list: {
name: String,
notes: String,
},
totalAmount: {
type: Number,
required: true,
},
payment: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
addressNote: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
Here in "list" I would like it to be an array that receives cart model information
Can I do this through ref? What would be the best possible way?
I have an order model/schema, and my goal is that in this model in "list", I receive the information from the model cart in array format. How could I do this using ref?
cart model
const mongoose = require('mongoose');
const CartSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
note: {
type: String,
required: true,
},
price: {
type: Number,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Cart", CartSchema);
order
const mongoose = require('mongoose');
const OrderSchema = new mongoose.Schema(
{
list: {
name: String,
notes: String,
},
totalAmount: {
type: Number,
required: true,
},
payment: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
addressNote: {
type: String,
required: false,
},
createdAt: {
type: Date,
default: Date.now,
}
},
{ timestamps: true }
);
module.exports = mongoose.model("Order", OrderSchema);
Basically receive in array format the information of the model cart in "list" in model order
You should define your list as an array of ObjectIds referring to the Cart model:
const OrderSchema = new mongoose.Schema(
{
list: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Cart'
}],
...
});
Then, to retrieve the values in list, just populate the Order:
Order.find({}).populate('list').exec();
Im relatively new to MongoDB and Mongoose. Im much used to MySQL so in used to inner joining tables on calls. Ive read a lot that you can link two Mongoose Schemas to achieve the same outcome. How would like like the two schemas together to when I make a call to get a chore by id it'll return the chore and then for the assignedTo & createdBy have the user scheme data for the said userId?
Chore Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ChoreSchema = new Schema({
title: {
type: String,
required: true
},
desc: {
type: String,
required: true
},
time: {
type: Number,
required: true
},
reaccurance: {
type: [{
type: String,
enum: ['Daily', 'Weekly', 'Bi-Weekly', 'Monthly']
}]
},
reward: {
type: Number,
required: true
},
retryDeduction: {
type: Number,
required: false
},
createdDate: {
type: Date,
default: Date.now
},
createdBy: {
type: String,
required: true
},
dueDate: {
type: Date,
required: true
},
status: {
type: [{
type: String,
enum: ['new', 'pending', 'rejected', 'completed', 'pastDue']
}],
default: ['new']
},
retryCount: {
type: Number,
default: 0,
required: false
},
rejectedReason: {
type: String,
required: false
},
familyId: {
type: String,
required: true
},
assignedTo: {
type: String,
required: false,
default: ""
}
});
let Chores = module.exports = mongoose.model('Chores', ChoreSchema);
module.exports.get = function (callback, limit) {
Chores.find(callback).limit(limit);
};
User Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
role: {
type: [{
type: String,
enum: ['Adult', 'Child']
}]
},
birthday: {
type: String,
required: false
},
familyId: {
type: String,
required: true
},
balance: {
type: Number,
required: true,
default: 0.00
}
});
let Users = module.exports = mongoose.model('Users', UserSchema);
module.exports.get = function (callback, limit) {
Users.find(callback).limit(limit);
};
Im trying to link ChoreSchema.createdBy & ChoreScheme.assignedTo by UserSchema._id
How I make the call in Node.js:
exports.index = function(req, res) {
Chore.get(function(err, chore) {
if (err)
res.send(err);
res.json({
message: 'Chore List',
data: chore
});
});
};
Mongoose has a more powerful alternative called populate(),
which lets you reference documents in other collections.
https://mongoosejs.com/docs/populate.html
Here is how you can link ChoreSchema.createdBy and ChoreScheme.assignedTo by UserSchema._id
var mongoose = require('mongoose');
const { Schema, Types } = mongoose;
var UserSchema = new Schema({
firstName: { type: String, required: true },
...
})
var ChoreSchema = new Schema({
title: { type: String, required: true },
...
//The ref option is what tells Mongoose which model to use during population
assignedTo: { type: Types.ObjectId, ref: 'Users' },
createdBy: { type: Types.ObjectId, ref: 'Users' },
})
let Chores = mongoose.model('Chores', ChoreSchema);
let Users = mongoose.model('Users', UserSchema);
Then in your express route handler you can populate assignedTo & createdBy like this
router.get('/chores/:id', function (req, res) {
const choreId = req.params.id;
Chores.find({ _id: choreId })
.populate('createdBy') // populate createdBy
.populate('assignedTo') // populate assignedTo
.exec(function (err, chore) {
if(err) {
return res.send(err)
}
res.json({ message: 'Chore List', data: chore });
});
})
This should be a simple problem. I've formatted my 'update' exactly how it is on mongo's documentation, as you can see:
Message.update(
{ id: messageID },
{ $inc: { votes: 1 } }
);
I am trying to increase 'votes' by 1. Yet it is not changing. Here is my schema file:
var mongoose = require('mongoose');
var MessageSchema = new mongoose.Schema({
room: {
type: String,
required: true
},
timestamp: {
type: Object,
required: true
},
votes: {
type: Number
},
id: {
type: String,
required: true
},
uid: {
type: String,
required: true
},
parent: {
type: String,
required: true
},
text: {
type: String,
required: true
}
});
module.exports = mongoose.model('messages', MessageSchema);
The input 'messageID' is correct, as the 'find' method works perfectly well with this table.
Try with findByIdAndUpdate instead of update . Hope it works
Message.findByIdAndUpdate(messageID ,{ $inc: { votes: 1 }},function(err, response) {
res.send(response);
});