Posts

Showing posts with the label mongoose

Use Rename collection in mongoose

Image
Clash Royale CLAN TAG #URR8PPP Use Rename collection in mongoose I have a problem. I renamed my collection responses into old. It works really well but now I need to retrieve my data and my impediment is that I only used the model to retrieve my data from a collection. But now I need to retrieve my data from my renamed collection but I have no model and schema. I tried to create a schema and a model but it didn't work. It returns no elements. Here is a part of the code : app.get("/Play", function (req, res) { var urlTempBox = 'http://localhost:3000/Play'; /////////////////////////////////////////////////////////// request(urlTempBox,function(error, response, body) { if (error) { throw(error); } else{ var jobj = JSON.parse(response.body); persistRS(jobj); setTimeout(function() { ResponseDatabase.find() .populate('unitCode') .exec(function(err, finalData){ ...

Populate document with other fields

Image
Clash Royale CLAN TAG #URR8PPP Populate document with other fields I got the following document: { "_id": "5b5df332879b4769739e3ed0", "name": "Back workout", "exercises": [{ "_id": "5b5df9a7879b4769739e3ed1", "results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}] }] } I want to populate the exercise based on the _id and also append the results data. _id results Expected output: { "_id": "5b5df332879b4769739e3ed0", "name": "Back workout", "exercises": [{ "_id": "5b5df9a7879b4769739e3ed1", "name": "EXERCISE_NAME", "results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}] }] } My Schema looks ...

simplifying deep nested mongo query without using aggregate

Image
Clash Royale CLAN TAG #URR8PPP simplifying deep nested mongo query without using aggregate I'm trying to create a sort of newsfeed feature for my app. I'm trying to understand how to do it with a nested query in Mongo/Mongoose rather than using aggregate. (the rest of the app uses nested queries) and I'd prefer to not have to modify the query with vanilla javascript to get the perfect object if I don't need to. I'd like to get the friends of the current user, for each friend, get all of their posts and then sort it all by date. I want to make my current query more efficient as there's an extra step my query in Mongoose User.findOne({ _id: req.userId }, 'friends.user -_id') .populate({ path: 'friends.user', model: 'User', select: 'posts -_id', populate: { path: 'posts', model: 'Post', select: 'date author user desc -_id', ...

Mongoose ValidationError when there's no collection

Mongoose ValidationError when there's no collection Model.insertMany fails, returns a validation error when the collection doesn't exist, then creates the collection. I want it to create the collection and insert my document when collection doesn't exist, how can i do that? I don't have any problems inserting the same data after collection is created. ValidationError: Video validation failed: time: Path `time` is required., thumbnail: Path `thumbnail` is required., duration: Path `duration` is required., views: Path `views` is required., description: Path `description` is required., title: Path `title` is required., yt_id: Path `yt_id` is required. at MongooseError.ValidationError Here's my model: var VideoSchema = new mongoose.Schema({ _id: { type: String, maxlength: 11, default: function(){ nanoid(11); } }, yt_id: { type: String, maxlength: 11, required: true, trim: true },...