Matching user in Mongoose

Hello,

I hope everyone learning something today. I need some help. I have a Gift model which looks something like this:

let giftSchema = new mongoose.Schema({
	gift: String,
    date: {
        type: Date
    },
	giftDescription: String,
	giftAmount: String,
	giftCode: String,
	redeemCode: String,
	passCode: String,
	senderFirstName: String,
	senderLastName: String,
	giftMessage: String,
    user: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        username: String
    }
});

module.exports = mongoose.model('Gift', giftSchema);

This is my user model:

const mongoose = require('mongoose'),
      passportLocalMongoose = require('passport-local-mongoose');

let UserSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    alaisFirstName: String,
    alaisLastName: String,
    username: String,
    password: String,
    isAdmin: Boolean,
    addressLine1: String,
    addressLine2: String,
    city: String,
    state: String,
    zipCode: String
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', UserSchema);

I have pretty much 2 users, and no guest. The admin sends gifts to a user. So as an admin, on the new route, I need to send something to the user. However, in my field: for ( think gmail search field ) I will be searching for either firstName, lastName, alaisFirstName, and alaisLastName than sending the gift to the user.

How could I model that? I cant do it by ID because the admin is the one sending it to the user, but I want something similar to that.

Tis the season for gift giving! It sounds like you’re trying to lookup a user, but you won’t have access to the _id parameter as an admin?

If you’re looking up a single user, then you’ll want a field unique to that user, so you could use something like your username field right? One thing you’ll want to do in your model to prevent duplicate usernames is to add the option unique and you can also trim the username (remove whitespace from both sides of the string). If someone tries to signup with a duplicate username, the method will return an error which you can pass along to the client.

{ username: 
  {
     type: String, 
     unique: true,
     trim: true
  }
}

When you’re looking up the username, use the Mongoose method to find a single item as a best practice. So in the example where you’re looking up a user and the user object has a gifts array where gifts are stored, you might do something like this:

User.findOneAndUpdate({ username: "joe" }, { $push: { gifts: newGift } }, { new: true }, function (error, user) {
    if (error) {
       // Handle error
    }
    res.status(200).send({ message: "Gift added!", user: user });
});

Note that the “new” option instructs mongoose to return the updated user document in the callback so you can send it in your response. Hope this is helpful!

1 Like

Thank you so much @jmcilhargey, the findOneAndUpdate method is what got it for me. :slight_smile: Thank you for being awesome, and taking the time to look at my problem. It really made it clear for me.