mongoose

Sorting records in a way so that only records which matches an id comes first?

大城市里の小女人 提交于 2021-01-28 22:05:02
问题 I have a query which return me the records or messages for a certain scenario: const [messages, messageCount] = await Promise.all([ MessageModel.find(params).select(filterObject).limit(ctx.query.limit).skip(ctx.paginate.skip) .sort('-sex -age') .lean(), MessageModel.countDocuments(params), ]); Is there any way to get the records first which matches an object id, and then rest of the records? Result { _id:abc0aa8573bfa917b152cdbc isPrivate:false message:"My name is stark" gender:"unisex" age:

Sorting records in a way so that only records which matches an id comes first?

雨燕双飞 提交于 2021-01-28 22:03:33
问题 I have a query which return me the records or messages for a certain scenario: const [messages, messageCount] = await Promise.all([ MessageModel.find(params).select(filterObject).limit(ctx.query.limit).skip(ctx.paginate.skip) .sort('-sex -age') .lean(), MessageModel.countDocuments(params), ]); Is there any way to get the records first which matches an object id, and then rest of the records? Result { _id:abc0aa8573bfa917b152cdbc isPrivate:false message:"My name is stark" gender:"unisex" age:

How to calculate the percentage using facet in MongoDB?

瘦欲@ 提交于 2021-01-28 21:31:31
问题 I am calculating the notification percentage in my app for tracking some statistics. My Collection: [ { _id: "123", status: "seen", userId: "589" }, { _id: "223", status: "seen", userId: "589" }, { _id: "474", status: "unseen", userId: "589" }, { _id: "875", status: "seen", userId: "112" }, { _id: "891", status: "unseen", userId: "112" } ] Expected Result: Here we can see that, UserId - 589 has received 3 notifications out of which 2 are seen. So the calculation is (totalNumOfSeen

How to populate one to many relationship in mongoose with parent refrencing?

会有一股神秘感。 提交于 2021-01-28 21:31:06
问题 I have three collections: parent, child1, child2. parent collection: [{_id:1}, {_id:2}, {_id:3}] child1 collection: [{_id:1, child1:'a', parent:1}, {_id:2, child1:'b', parent:1}, {_id:3, child1:'c', parent:2}] child2 collection: [{_id:1, child1:'d', parent:2}, {_id:2, child1:'e', parent:3}] collections child1 and child2 referenced to parent collection. now, I want a query in mongoose to get a result like this: [ { _id:1, child1:[{_id:1, child1:'a'}, {_id:2, child1:'b'}], child2:[], }, { _id:2

Mongoose object id constraint

喜你入骨 提交于 2021-01-28 21:13:48
问题 hello I'm working with mongoose and after a long time I still dont know how to fix this error... I tried during several hours to fix it but every time I just 'hide' the problem by complete the field with random character but for my project I cant, I need a precise Id so if someone could help with an example if he can, it will be really nice Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters I m trying to create an Id like : map1AssigneSeat And to

MongoDB $group (mongo playground)

[亡魂溺海] 提交于 2021-01-28 19:36:36
问题 I have asked a question here about the $unwind operator, but am facing issues with grouping the data properly after. I have a mongo playground with an example, but here it is also. After an $unwind , $lookup and $group in the query (maybe there is a better, more efficient way to do it?), I am left with this data: [ { "ExerciseDetail": [ [{ "Name": "Squat", "_id": "5f60c3b7f93d8e00a1cdf414" }], [{ "Name": "Deadlift", "_id": "5f60c3b7f93d8e00a1cdf415" }] ], "Sets": [ { "ExerciseId":

Mongoose add expires attribute for a specific field

浪尽此生 提交于 2021-01-28 19:05:46
问题 I have made a token based authentication system for my web application and I need to have an expiration date for the token field. The user model which keeps token is as follow: module.exports = (function() { var userSchema = new Schema({ phone: String, token: { value: { type: String, lowercase: true, trim: true } }, verificationCode: Number, createdAt: { type: Date, default: Date.now() } }); var User = mongoose.model('User', userSchema); return User; })(); I am wondering is there any way to

Querying for Object in Mongoose Sub Array

 ̄綄美尐妖づ 提交于 2021-01-28 18:43:16
问题 I have a Schema that looks like this: var LibrarySchema = new Schema({ id: String, contactNumber: String, collections: [{ id: String, description: String subCollections: [{ id: String, description: String, recentlyUpdated: Boolean }, { id: String, description: String, recentlyUpdated: Boolean }] }] }) module.exports = mongoose.model('Library', LibrarySchema); All the ID's are unique. There can be multiple Libraries inside a District (another array). My question is, how would I query into the

Aggregate and reduce a nested array based upon an ObjectId

倖福魔咒の 提交于 2021-01-28 14:29:55
问题 I have an Event document structured like so and I'm trying to query against the employeeResponses array to gather all responses (which may or may not exist) for a single employee: [ { ... eventDate: 2019-10-08T03:30:15.000+00:00, employeeResponses: [ { _id:"5d978d372f263f41cc624727", response: "Available to work.", notes: "" }, ...etc ]; } ]; My current mongoose aggregation is: const eventResponses = await Event.aggregate([ { // find all events for a selected month $match: { eventDate: { $gte

Remove a specific nested object from an array of objects

守給你的承諾、 提交于 2021-01-28 14:17:51
问题 I need to remove a specific object that is nested inside an array of objects. The following db structure looks like: I would like to remove one of the teams based on the roomId (to find the specific room) and based on the team approved state. If a team has "approved: false" it needs to be deleted from the array of objects. I'm using mongoose and came up with the following, but without succes: Room.update({"roomId": req.params.roomId}, {"$pull": { "teams.approved": false } }) screenshot thats