mongoose duplicated key error with empty string - javascript

I have a schema like this:
const MemberSchema = mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
unique: true
},
phone: {
type: String,
unique: true
}
})
and of course email and phone needs to be unique but the thing is either email or phone is optional so they can be empty string.
and when you set them as unique index I get this error
E11000 duplicate key error collection: model.members index: email_1 dup key: { : "" }'
Is there any way to add exception to unique?
Edit
I changed my code like below but still doesn't work.
email: {
type: String,
trim: true, index: true, unique: true, sparse: true
},
\
'E11000 duplicate key error collection: email-collector.members index: email_1 dup key:{ : null }'
when I run getIndexes, I get
> db.members.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "email-collector.members"
},
{
"v" : 2,
"unique" : true,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "email-collector.members",
"sparse" : true,
"background" : true
},
{
"v" : 2,
"unique" : true,
"key" : {
"email" : 1,
"phone" : 1
},
"name" : "email_1_phone_1",
"ns" : "email-collector.members",
"sparse" : true
}
]

Related

MongoError: E11000 duplicate key error collection with passport

I have the following userSchema
var mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose");
var userSchema = new mongoose.Schema({
email: String,
password: String,
userName: String,
fname: String,
lname: String,
userType: Number,
subscribedThreads: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Threads"
}
]
});
// add passport methods
userSchema.plugin(passportLocalMongoose);
// export modules to be used by the file requiring it
module.exports = mongoose.model("Users",userSchema);
The first entry into the collection occurs as it should but the next ones give
{ [MongoError: E11000 duplicate key error collection: KManV3.users
index: username_1 dup key: { : null }]
name: 'MongoError',
message: 'E11000 duplicate key error collection: KManV3.users index:
username_1 dup key: { : null }',
driver: true,
code: 11000,
index: 0,
errmsg: 'E11000 duplicate key error collection: KManV3.users index:
username_1 dup key: { : null }',
getOperation: [Function],
toJSON: [Function],
toString: [Function]
}
Also, dbname.users.getIndexes() gives:
> db.users.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "KManV3.users"
},
{
"v" : 1,
"key" : {
"password" : 1
},
"name" : "password_1",
"ns" : "KManV3.users",
"background" : true
},
{
"v" : 1,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "KManV3.users",
"background" : true
}
]
Apparently every property of schema has been set as unique and I can't add data into collections even if the data is totally different. I'm not sure if it's due to integration of passport.
Looking at the options for passport-local-mongoose:
usernameField: specifies the field name that holds the username. Defaults to 'username'.
usernameUnique : specifies if the username field should be enforced to be unique by a mongodb index or not. Defaults to true.
Which explains why your collection has a unique index on the (non-existent-in-your-schema) username field.
If you don't actually set this field in documents that you add to the database, MongoDB will use null, and once the first document has been inserted, a subsequent document (also with the field value null for username) will throw an E11000 error.
So first, remove the index on username (and also password, I assume you once marked that field as unique in your schema), and set the proper field name for passport-local-mongoose to use:
userSchema.plugin(passportLocalMongoose, { usernameField : 'userName' });
(or email, if you want that field to be used as unique user identifier)
Error reason - The index is not present in your collection, in which you are trying to insert.
Solution - drop that collection and run your program again.

mongoose update nested document not working

I try to figure out what I'm doing wrong that moongose is not updating my document. Just for testing the id's are hardcoded.
node.js
var id1 = "592fd471fedd311d5c76a024";
var id2 = "592fd4ad608e001d79938ba8";
Workshop.update({
_id: id1,
'themen._id': id2
}, {
'$set': {
'themen.$.risikolandschaft': ["John", "Doe"],
}
},
function(err, body) {
if (err) {
console.log(err)
} else {
console.log(body);
}
}
);
here my data copied from MongoDB Compass
{
"_id" : ObjectId("592fd471fedd311d5c76a024"),
"clientid" : "592cff8794738f0347609666",
"bezeichnung" : "Workshop 1.5.2017 / 10:46",
"themen" : [
{
"thema" : {
"__v" : 0,
"bezeichnung" : "Sturm",
"beschreibung" : "Text",
"_id" : "59255757b1485d0ad2a6924f"
},
"risikolandschaft" : [ "one", "two", "three"],
"date" : "2017-06-01T08:47:41.944Z",
"_id" : ObjectId("592fd4ad608e001d79938ba8")
}
], ...
the log from body
{ n: 0, nModified: 0, ok: 1 }
for adding a new item to the "themen" subdocument i use this:
var new_thema = {
_id: mongoose.Types.ObjectId(),
date : req.body.date,
risikolandschaft : req.body.risikolandschaft,
thema : req.body.thema };
here the mongoose schema
var WorkshopSchema = mongoose.Schema({
clientid: {
type: String,
required: true
},
bezeichnung: {
type: String,
required: true
},
stammdaten : [],
date : Date,
themen : []
});
and there is no subschema for "themen"
"mongoose": "^4.8.6"

How to compare and push field to sub array in object

I have an object which contain name and id. Second object has some fields and that id field from first object.
For Example:
FirstObj = [{
_id: '48765465f42424',
Name : 'Sample'
},{
_id: '48765465f654654',
Name : 'Sample1'
}]
secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : { IsActive : true,
FirstObjid : '48765465f42424'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f654654'
}
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : { IsActive : true,
FirstObjid : '48765465f654654'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f42424'
},
}]
}]
Now I need to compare both and push that name from first object to secondobj's subobj beside FirstObjid field .
Expected Output is :
secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : { IsActive : true,
FirstObjid : '48765465f42424',
Name : 'Sample'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f654654',
Name : 'Sample1'
}
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : { IsActive : true,
FirstObjid : '48765465f654654',
Name : 'Sample1'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f42424',
Name : 'Sample'
},
}]
}]
How can I achieve it.
Try this
secondObj.subarray.forEach(function(item){
if(item.FirstObjid==FirstObj._id)
item.Name = FirstObj.Name
})
Use map function and compare the object ids like this
var secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : 'sfsadf',
FirstObjid : '48765465f42424'
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : 'awrfsa',
FirstObjid : '48765465f654654'
}]
}]
var FirstObj = [{
_id: '48765465f42424',
Name : 'Sample'
},{
_id: '48765465f654654',
Name : 'Sample1'
}]
secondObj.subarray = secondObj.map(o => o.subarray.map(k=> FirstObj.map(l=> {
if(l._id == k.FirstObjid){
k.Name = l.Name
}
return o;
})))
console.log(secondObj)

Mongo db reference duplicate?

I have this error i cannot seem to understand what I'm doing wrong.
In my database i have an object called Question, questions have a reference to Subject and to User. When I'm trying to post to Question i get a strange error.
E11000 duplicate key error index: codenoname.questions.$subject.name_1 dup key: { : null }
My Question schema:
var questionSchema = mongoose.Schema({
title: { type : String , required : true},
text: { type : String , required : true},
subject: {type:String, ref: 'Subject', required: true},
createdBy: {type: String, ref:'User', required: true},
difficulty: { type : String , required : true},
alternatives: [{alternative: {type:String, required:true}, isCorrect: {type:Boolean, required:true}}]
});
and my Subject
var subjectSchema = mongoose.Schema({
name: { type : String , required : true, unique:true}
});
Save method:
var question = new Question(
{title: title,
text: text,
subject: ObjectId(subject),
difficulty: difficulty,
createdBy: id,
alternatives:alternatives
});
question.save( function(err, newQuestion) {
if(err) {
res.status(400).json({err:err});
} else {
res.status(200).json({status:"Question added"});
}
});
What i have tried
Delete all Questions, then I can post, but just one...
Remove the reference and just keep it as a string. No difference.
Restarted the server a few times.
Try removing the unique: true from subject. I think questionSchema inherits the unique property and once you try to save two different questions with the same subject, you'll get duplicate key.
Follow these steps:
Remove unique: true from your model
Find the index name by typing db.questions.getIndexes() in your terminal.
Remove it by typing db.questions.dropIndex(name) where name is the "name"-property from step 2
Example from my database where i'll remove the unique-property from usernames:
> db.accounts.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "passport_local_mongoose_express4.accounts"
},
{
"v" : 1,
"unique" : true,
"key" : {
"email" : 1
},
"name" : "email_1",
"ns" : "passport_local_mongoose_express4.accounts",
"background" : true
},
{
"v" : 1,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "passport_local_mongoose_express4.accounts",
"background" : true
}
]
> db.accounts.dropIndex('username_1')

How to sort inner array and match elements in mongoose

So I am trying to do a semi-complicated query in mongoose. It is as follows:
Event.findOne({ users: { $elemMatch: { user: someUserId, going: 1 }, sort: {createTime: -1} } }
Basically what I would like to do is find a single Event that has a user in its user array that matches an Id and is attending. I have multiple user records for each user in the user array so I want to find the most recent one, to do this I would like to sort the user array by createTime. This is where the error is coming in and it just returns undefined. It works just fine when I don't include the sort function, is there any way to include that?
Here is what my Event object looks like:
{
_id: 1,
endTime: 1429060173865,
createTime: 1429051773902,
startTime: 1429052973865,
description: 'some desc',
creator: 2,
users:
[ { user: 1,
status: 1,
going: 1,
createTime: 1429051773749,
_id: 552d997d8e923847306e2c21 },
{ user: 1,
status: 1,
going: 1,
createTime: 1429051773922,
_id: 552d997d8e923847306e2c25 },
{ user: 1,
status: 9,
going: 0,
createTime: 1429051773942,
_id: 552d997d8e923847306e2c26 } ],
destroyed: 0 }
Is there any way to make this query entirely in mongoose?
As part of find, MongoDB can't sort an array field of a document. You could define a virtual of Mongoose to return the array in sorted order. You could also maintain the array in sorted order, as shown below:
> db.test.drop()
// note: not in order when inserted
> db.test.insert({ "_id" : 0, "users" : [
{ "user" : 1, "going" : 1, "created" : 22 },
{ "user" : 2, "going" : 1, "created" : 775 },
{ "user" : 1, "going" : 1, "created" : 6432 }
] })
// insert user to array and sort as part of update
> db.test.update({ "_id" : 0 },
{ "$push" : {
"users" : {
"$each" : [{ "user" : 2, "going" : 1, "created" : 5532 }],
"$sort" : { "created" : -1 }
}
} })
> > db.test.findOne()
{
"_id" : 0,
"users" : [
{ "user" : 1, "going" : 1, "created" : 6432 },
{ "user" : 2, "going" : 1, "created" : 5532 },
{ "user" : 2, "going" : 1, "created" : 775 },
{ "user" : 1, "going" : 1, "created" : 22 }
]
}
That way, when you perform your find query, the arrays in the matching documents will already be in the desired order.
Your query is not correctly written. In order to sort you should write it in third argument of find:
Event.findOne(
{ users:
{ $elemMatch: { user: someUserId, going: 1 }}
},
null,
{
sort: {createTime: -1}
},
function(err, event) {
//your event here
});

Categories

Resources