I have a User document which has a Notes subdocument.
I'm using the following code to push new notes for the user with the given email address.
UserSchema.statics.addNotesToUser = function (email, notes, callback) {
return this.updateOne(
{'email': email},
{$push: {notes}},
callback
)
};
This is working fine, however it's ignoring my unique constraint on the NoteSchema. these are my schemas
const NoteSchema = new Schema({
_id: false,
id: {type: String, required: true, trim: true, unique: true},
content: {type: String, required: true, trim: true, lowercase: true},
added: {type: Date, default: Date.now},
used: {type: Date, default: Date.now},
book: {
name: {type: String, required: true}
}
});
const UserSchema = new Schema({
email: {type: String, required: true, trim: true, lowercase: true, unique: true},
notes: [NoteSchema]
});
I'm wondering how I can make sure that when pushing new notes to my user, I can validate if the ID of the notes is unique.
Thank you.
To achieve uniqueness constraint like functionality in subdocuments, hope that's OK.
let notesId = [];
notes.forEach(function(val,index){
notesId.push(val.id)
})
db.yourCollection.update(
{ 'email': email, 'NoteSchema.id': { '$ne': { $each: notesId } }},
{$push: {notes} },
callback)
Related
I wanted to save the data in "messageSchema" which is sub document of chatSchema by checking the "receiver" of chatSchema and "username" of userSchema.
like pseudoCode:-
if(userSchema.username == "Rahul" && userSchema.chatSchema.receiver){
then save the data in chatSchema.message;
}
Here is my Schema:-
var messageSchema = mongoose.Schema({
messageId: {type: String, unique: true, required: true},
created: {type: Date, default: Date.now},
messageContent: String
});
var chatSchema = mongoose.Schema({
message: [messageSchema],
receiver: {type: String, required: true}
});
var userSchema = mongoose.Schema({
username: { type: String, unique: true, required: true },
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
token: { type: String, required: false },
conversations: [chatSchema]
});
please suggest what should be code to save the message data.
tried below one that didn't work.
User.findOneAndUpdate({username: "rahul", "conversations.receiver": data.receiver },{$push: {"conversations.message": message}});
I think you need to use $elemMatch instead of the dot notation for matching properties within an array. Try this:
User.findOneAndUpdate(
{
username: "rahul",
conversations: {
$elemMatch: { receiver: data.receiver }
}
},
// or whatever your update is
{$push: {"conversations.message": message}
})
I'm sorry if this might be a duplicate question but I'm quite having a hard time understanding Mongoose. I am working on a Node.js project that implements Mongoose and MongoDB. What I want to accomplish is to modify and save some users' data through a call from a specific endpoint.
Mongoose Schema looks like this
var UserSchema = new Schema({
isAdmin: {type: Boolean, default: false},
name: String,
surname: String,
nickname: { type: String },
email: { type: String, lowercase: true, required: true, trim: true, unique: true, dropDubs: true },
password: { type: String, required: true },
salt: { type: String },
verified: { type: Boolean, default: false },
bio: {
type: { type: String, enum: [0,1] }, // 0='Appassionato', 1='Giocatore'
birthday: String,
height: Number,
number: Number,
role: { type: String, enum: [0,1,2,3] }, // 0='Playmaker', 1='Ala', 2='Guardia', 3='Centro'
team: String,
city: String,
aboutMe: String,
},
newsletter: {type: Boolean, default: false},
lastCheckin: {type: mongoose.Schema.Types.ObjectId, ref: 'Checkin'},
follows: [{type: mongoose.Schema.Types.ObjectId, ref: 'Structure'}],
score: { type: Number, default: 0 },
profilePicture: String,
lastLogin: {type: Date},
facebook: {
id: String,
accessToken: String,
profileImage : String
}
}, {
collection: 'users',
retainKeyOrder: true,
timestamps: true,
}).plugin(mongoosePaginate);
Following is the code for when the endpoint gets interrogated
exports.updateUser = (req,res) => {
var userId = req.params.userId;
var updates = req.body;
User.findOneAndUpdate({_id: userId}, {$set: updates}, (err, saved) => {
if (!err) {
console.log("Ritorno questo: " + saved);
return res.status(202).json(saved);
} else {
return res.status(500).json(saved);
}
});
};
As far as I understood, the method findOneAndUpdate exposed by Mongoose should find the document I'm looking for and then modify it and save it. This doesn't happen though.
Through PostMan I'm sending this JSON
{"bio.aboutMe":"Hello this is just a brief description about me"}
But PostMan is responding with the non-modified object. What am I missing here?
What you need to do is to add {new:true}, it give you back the updated document.
In the documentation :
If we do need the document returned in our application there is
another, often better, option:
> Tank.findByIdAndUpdate(id, { $set: { size: 'large' }}, { new: true },
> function (err, tank) { if (err) return handleError(err);
> res.send(tank); });
This is something I don't really like as there is another option if we don't want to have the document → update
So what you need to do is :
User.findOneAndUpdate({_id: userId}, {$set: updates}, {new:true}.....
I was wondering if anyone could help me add a dynamic date to my database. The idea is I want to sort the database by people who have registered. So below I have a user schema of the values my database holds I have tried different things like adding a variable that holds a date object but no luck. I have tried searching around with no clear answer. Is there someone that can help me out?
var userSchema = new Schema({
firstName: {type: String, required: true, validate: nameValidator},
lastName: {type: String, required: true, validate: nameValidator},
addressOne: {type: String, required: true, validate: addressValidator},
addressTwo: {type: String, validate: addressValidator},
city: {type: String, required: true, validate: cityValidator},
state: {type: String, required: true, validate: stateValidator},
zipcode: {type: String, required: true, validate: zipValidator},
country: {type: String, required: true, validate: countryValidator},
});
You can specify date datatype using Date. All supported data types can be found here.
var userSchema = new Schema({
firstName: {
type: String,
required: true,
validate: nameValidator
},
lastName: {
type: String,
required: true,
validate: nameValidator
},
registrationDate: {
type: Date,
required: false,
default: Date.now
}
);
Above code will also give registrationDate as current time of saving userSchema by default.
I am working on a project for my internship, where they are using node.js and mongoDB in their back-end. This set up really doesn't work so well at the moment, because we in fact have relational data and mongoDB really isn't the best for that.
My problem is, I have to get some data from the DB that require a "join" on two tables. MongoDB doesn't allow this, so I am trying to do it manually.
I have to find a gig, and for each job type (JobTypeLine) the gig has, find the info about staff members invited. RolodexMembersInterested is an array that contains IDs which allows us to find the staff info in the RolodexMember "table".
The json structures look like this:
Gig:
{
_id: {type: Schema.ObjectId},
CreatedAt: {type: Date, default: Date.now()},
EventName: {type: String, required: true},
Headline: {type: String, required: true},
Date: {type: Date, required: false},
EndDate: {type: Date, required: false},
Address: {type: String, required: false},
Description: {type: String, required: true},
CompanyName: {type: String, required: true},
CompanyID: {type: String, required: true},
Exposed: {type: Boolean, default: false},
HashTags: {type: []},
JobTypeLine: {type: [
{
JobType: {type: String, required: true},
Briefing: {type: String, required: false},
Quantity: {type: Number, required: true},
StartTime: {type: String, required: true},
EndTime: {type: String, required: true},
NumberOfHours: {type: String, required: false},
HourlySalary: {type: Number, required: false},
RolodexMembersInvited: {type: [], default: []},
RolodexMembersAssigned: {type: [], default: []},
RolodexMembersInterested: {type: [], default: []}
}
]},
},
RolodexMember:
{
_id:{type:Schema.ObjectId},
companyId: {type: String, required: true},
userId: {type: String, required: false},
userEmail: {type: String, required: true},
comment: {type: String, required: false},
payRate: {type: Number, required: false},
name: {type: String, required: false},
phone: {type: String, required: false},
pictureUrl: {type: String, required: false},
isHot: {type: Boolean, required: false},
isInPaySystem: {type: Boolean, required: false},
numberOfPreviousGigs: {type: Number, required: true, default: 0},
previousGigs: [],
otherAttributes: [
{
attributeName: {type: String, required: false },
attributeValue: {type: String, required: false }
}
],
})
So I wrote a method to "simulate" my join :
exports.getMembersInvitedToGig = function(req,res) {
var jobTypes = [];
var rolodexInvited = [];
gigsModel.findOne({_id: req.body.gigId}, function(err, gig) {
if(err) {
console.log(err);
res.send(404, "Couldn't find the gig with id: " + req.body.gigId);
} else {
for(var i=0;i<gig.JobTypeLine.length;i++){
rolodexInvited = [];
for(var j=0;j<gig.JobTypeLine[i].RolodexMembersInvited.length;j++) {
var userId = gig.JobTypeLine[i].RolodexMembersInvited[j];
getUserData(userId, function(data,success) {
if (success) {
rolodexInvited[j] = data;
}
});
}
jobTypes[i] = rolodexInvited;
}
res.send(200,jobTypes);
}
});
}
function getUserData (userId, callback) {
var RolodexItem = mongoose.model('RolodexItems');
RolodexItem.findOne({ _id: userId }, function (err, doc) {
if (err) {
console.log("There was an error in finding the rolodex item: " + err);
} else {
callback(doc,true);
}
});
}
My problem is that the method return and empty array filled other empty arrays (as many as I have jobTypes). I know the getUserData function works just fine and finds the right staff info etc. the problem is, it seems like node doesn't wait for the data to be returned inside the for(j) loop and just continues on (and eventually ends gets to the res.send(200,jobTypes) while jobTypes is actually still empty. I have tried adding a callback to the getUserData to solve that problem, but it didn't change anything.
(Now I know using a relational database would solve the problem all together, but I don't really get a say in this, so I have to make this work as it is). There might also be some really bad practices in there, as I just based myself on functions previously made by other people, and I have noticed they weren't really good at it. I have never worked with node.js previously though, so I haven't had time to look as some proper practices/etc. (Feel free to point out anything that is badly written, I'd love to learn the "good" way to do it)
The problem is that your getUserData function is asynchronous (because findOne is asynchronous). So your for loop does not wait for your getUserData function to complete.
You should use the async library (have a look to the each function) : https://github.com/caolan/async
I have defined a mongoose user schema:
var userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true},
password: { type: String, required: true},
name: {
first: { type: String, required: true, trim: true},
last: { type: String, required: true, trim: true}
},
phone: Number,
lists: [listSchema],
friends: [mongoose.Types.ObjectId],
accessToken: { type: String } // Used for Remember Me
});
var listSchema = new mongoose.Schema({
name: String,
description: String,
contents: [contentSchema],
created: {type: Date, default:Date.now}
});
var contentSchema = new mongoose.Schema({
name: String,
quantity: String,
complete: Boolean
});
exports.User = mongoose.model('User', userSchema);
the friends parameter is defined as an array of Object IDs.
So in other words, a user will have an array containing the IDs of other users. I am not sure if this is the proper notation for doing this.
I am trying to push a new Friend to the friend array of the current user:
user = req.user;
console.log("adding friend to db");
models.User.findOne({'email': req.params.email}, '_id', function(err, newFriend){
models.User.findOne({'_id': user._id}, function(err, user){
if (err) { return next(err); }
user.friends.push(newFriend);
});
});
however this gives me the following error:
TypeError: Object 531975a04179b4200064daf0 has no method 'cast'
If you want to use Mongoose populate feature, you should do:
var userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true},
password: { type: String, required: true},
name: {
first: { type: String, required: true, trim: true},
last: { type: String, required: true, trim: true}
},
phone: Number,
lists: [listSchema],
friends: [{ type : ObjectId, ref: 'User' }],
accessToken: { type: String } // Used for Remember Me
});
exports.User = mongoose.model('User', userSchema);
This way you can do this query:
var User = schemas.User;
User
.find()
.populate('friends')
.exec(...)
You'll see that each User will have an array of Users (this user's friends).
And the correct way to insert is like Gabor said:
user.friends.push(newFriend._id);
I'm new to Mongoose myself, so I'm not entirely sure this is right. However, you appear to have written:
friends: [mongoose.Types.ObjectId],
I believe the property you're looking for is actually found here:
friends: [mongoose.Schema.Types.ObjectId],
It may be that the docs have changed since you posted this question though. Apologies if that's the case. Please see the Mongoose SchemaTypes docs for more info and examples.
I would try this.
user.friends.push(newFriend._id);
or
friends: [userSchema],
but i'm not sure if this is correct.