I want to lookup for dynamic field of object
Schema
const settingsRef = new mongoose.Schema({
onsiteCounter: { ... },
onsiteOffCounter: { ... },
});
const DemoSchema = new mongoose.Schema({
settings: {
type: settingsRef,
default: {},
}
});
mongoose.model(Demo, DemoSchema);
Schema on which I'm aggregating
const OtherSchema = new mongoose.Schema({
siteLocation: {
type: String,
required: true,
}
})
mongoose.model(Other, OtherSchema);
What should be exact code here : "$eq": ["$settings", "$$serviceType"]
query
Other.aggregate([{
$lookup: {
"from": Demo.collection.collectionName,
"let": {
"serviceType": "$siteLocation"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": ["$settings", "$$serviceType"]
}
}
},
{
"$project": {
isAutoServiceChargeActive: 1,
serviceChargeType: 1,
serviceChargeAmount: 1,
serviceChargeCutOff: 1,
absoluteMinOrder: 1,
preSetTipType: 1,
preSetTipAmount: 1,
isAutoAcceptActive: 1
}
}
],
"as": "serviceTypeDetails"
}
}])
I'm getting serviceTypeDetails as empty array []. Thank you so much for help in advance!
I'm doing a simple follow friend functionality.
Please see my codes below:
Following schema:
{
"userId": { type: String },
"followers": [{ "followerId": type: String }],
"followings": [{ "followingId": type: String }],
}
User schema:
{
"fullName": { type: String }
}
Note: user 8 has 1 follower and 2 followings.
Now, my expected output should be like this:
"userId": 8,
"followers": [
{
"followerId": 4,
"fullName": "Rose Marriott",
},
{
"followerId": 5,
"fullName": "James Naismith",
}
],
"followings": [
{
"followingId": 1,
"fullName": "Russell Oakham",
},
{
"followingId": 5,
"fullName": "James Naismith",
}
]
This is what I tried so far:
db.followings.aggregate([
{ $unwind: "$followers" },
{
$lookup: {
from: "users",
localField: "followers.followerId",
foreignField: "_id",
as: "users"
}
},
{
$addFields:
{
users: { $arrayElemAt: ["$users", 0] },
},
},
{ $unwind: "$followings" },
{
$lookup: {
from: "users",
localField: "followings.followingId",
foreignField: "_id",
as: "users2"
}
},
{
$addFields:
{
users2: { $arrayElemAt: ["$users2", 0] },
},
},
{ $match: {"userId": mongoose.Types.ObjectId(userId) } },
{
$group: {
_id: "$_id",
userId: { $first: "$userId" },
followers: {
$push: {
followerId: "$followers.followerId",
fullName: "$users.fullName",
}
},
followings: {
$push: {
followingId: "$followings.followingId",
fullName: "$users2.fullName",
}
}
}
}
]);
But I'm getting 2 followers and 2 followings. I wonder what's causing this issue. Appreciate any help. Thanks!
You can try,
$addFields to make a unique array called userIds form both arrays followers and followings, $setUnion to get unique ids,
$lookup with users collection
$project to show fields,
followers get fullName, $map to iterate loop of followers and get the name of followerId from users array using $reduce and $cond
followings get fullName, $map to iterate loop of followings and get the name of followingId from users array using $reduce and $cond
db.followings.aggregate([
{
$addFields: {
userIds: {
$setUnion: [
{
$map: {
input: "$followers",
in: "$$this.followerId"
}
},
{
$map: {
input: "$followings",
in: "$$this.followingId"
}
}
]
}
}
},
{
$lookup: {
from: "users",
localField: "userIds",
foreignField: "_id",
as: "users"
}
},
{
$project: {
userId: 1,
followers: {
$map: {
input: "$followers",
as: "f",
in: {
$mergeObjects: [
"$$f",
{
fullName: {
$reduce: {
input: "$users",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$f.followerId"] },
"$$this.fullName",
"$$value"
]
}
}
}
}
]
}
}
},
followings: {
$map: {
input: "$followings",
as: "f",
in: {
$mergeObjects: [
"$$f",
{
fullName: {
$reduce: {
input: "$users",
initialValue: "",
in: {
$cond: [
{ $eq: ["$$this._id", "$$f.followingId"] },
"$$this.fullName",
"$$value"
]
}
}
}
}
]
}
}
}
}
}
])
Playground
when working with relations on Mongoose you should create the relationship based on a unique id, and then reference the document. In your case, it would be:
followingSchema = new mongoose.Schema({
{
"followers": [{type: mongoose.Schema.types.ObjectId, ref="User"}],
"followings": [{type: mongoose.Schema.types.ObjectId, ref="User"}],
}
})
userSchema = new mongoose.Schema({
fullname: String,
})
be aware that the userId will be created automatically by Mongoose with a field called _id. so, the end result of creating a new following documents would be:
{
_id: "klajdsfñalkdjf" //random characters created by mongoose,
followers: ["adfadf134234", "adfadte345"] //same as before, these are Ids of the users randomly generated by mongoose
followers: ["adfadf134234", "adfadte345"]
}
{
_id: adfadf134234,
fullName: "alex",
}
now, because there is no use for us to have a random number as information in the fields of following and followers in the following object, we can now use the method .populate() that can be used over the document itself to transform those Ids into actual information. You can see more about it here: mongoose documentation
our final result would be something like this:
{
_id: "añfakdlsfja",
followers : [{_id: "adlfadsfj", fullName: "alex"}],
following : [{_id: "adfadfad" , fullName: "other"}, {_id: "dagadga", fullName: "another"}]
}
I have some documents as following in db:
User A:
{
"_id" : ObjectId("5f1ec1869ea3e213cc2a159e"),
"age": 27,
"gender": "male",
"preference": {
"ageGroup": "25-35",
"gender": "female"
}
}
User X:
{
"_id" : ObjectId("378ec1869ea3e212333a159e"),
"age": 27,
"gender": "female",
"preference": {
"ageGroup": "20-30",
"gender": "male"
}
}
I am trying to filter docs based on :
other users' profile age and gender must match against the user's preference.
other users' preference also must match against the user's profile age and gender.
Here's what I am trying:
const getGsaMatches = async function (currentUser) {
const user: any = await User.findOne({ _id: currentUser._id });
const userPreference = user.preference;
const ageRange = userPreference.ageGroup.split("-");
const minAgeRange = ageRange[0];
const maxAgeRange = ageRange[1];
const matchResponse: any = await User.find({
"gender": userPreference.gender,
"age": { $gte: minAgeRange, $lte: maxAgeRange },
"preference.gender": user.gender,
"preference.ageGroup": user.age, // I'm stuck here
_id: { $ne: currentUser._id }
});
return matchResponse;
}
preference.ageGroup contains value in 25-30 string format.
How can I store this field so that it can be compared against a given single integer value?
I hope I made the problem clear.
A good way to start is to actually store it as an Integer. If you're using Mongo v4.0+ you can also use $toInt but if this is a query you're doing often then you might aswell save it in a structure like:
ageGroup: {
min: 20,
max: 30,
value: "20-30"
}
Now you can do something like this:
const matchResponse: any = await User.find({
_id: { $ne: currentUser._id },
"gender": userPreference.gender,
"age": { $gte: minAgeRange, $lte: maxAgeRange },
"preference.gender": user.gender,
"preference.ageGroup.min": {$lte: user.age},
"preference.ageGroup.max": {$gte: user.age}
});
Assuming you don't want to change the structure then you'll have to use an aggregation with $toInt as I suggested like so:
const matchResponse: any = await User.aggregate([
{
$addFields: {
tmpAgeField: {
$map: {
input: {$split: ["$preference.ageGroup", "-"]},
as: "age",
in: {$toInt: "$$age"}
}
}
}
},
{
$match: {
_id: { $ne: currentUser._id },
"gender": userPreference.gender,
"age": { $gte: minAgeRange, $lte: maxAgeRange },
"preference.gender": user.gender,
"tmpAgeField.0": {$lte: user.age},
"tmpAgeField.1": {$gte: user.age}
}
}
]);
I have following model
var reservationSchema = new Schema({
lcode: Number,
reserved_days: [daySchema],
});
var daySchema = new Schema({
pk: Number,
date: String,
price: Number,
payment_type: String,
payment_date: String,
room: { room_id: String, name: String }
});
I have a reservation with array of reserved_days. I need to compare this reservation and reservations in database and find reservation where at least one reserved_days date or reserved_days room.room_id are the same that contains in array. Array must not be fully equal, I just need to find duplicate days.
Now I try the following
Reservation.find({
$or: [{
"reserved_days.room.room_id": {
$in: req.body.reservation.reserved_days
}
}, {
"reserved_days.date": {
$in: req.body.reservation.reserved_days
}
}]
}).exec(function(err, found)
but it doesn't work.
Test request reservation.reserved_days object looks the following
"reserved_days": [
{
"pk": 3543,
"date": "2018-07-14",
"price": 3213.0,
"payment_type": "Безналичный расчет",
"payment_date": "2017-12-28",
"room": {
"room_id": "7",
"name": "Стандарт"
}
},
{
"pk": 3544,
"date": "2018-07-15",
"price": 3213.0,
"payment_type": "Безналичный расчет",
"payment_date": "2017-12-28",
"room": {
"room_id": "7",
"name": "Стандарт"
}
}]
Is it possible to do ?
I am new to mongodb, and playing around with a self-project, where user can subscribe to 3-4 different courses that are predefined. Each course is 1 hour long course everyday, and students can subscribe for either 15, 30 or more days.
App will store information of the students, the course they subscribed for (how many) days and days they were present for the course.
This is my Mongoose Schema.
var mongoose = require('mongoose');
var schemaOptions = {
timestamps: true,
toJSON: {
virtuals: true
}
};
var courseSchema = new mongoose.Schema({
name: String
});
var studentSchema = new mongoose.Schema({
name: String,
email: { type: String, unique: true},
phone: String,
gender: String,
age: String,
city: String,
street: String,
picture: String,
course: [courseSchema],
subscriptionDays: Number,
daysPresent: [Date]
}, schemaOptions);
module.exports = mongoose.model('Student', studentSchema);
Here, course is any of 3-4 courses, one student can subscribe one or more course at same time. subscriptionDays is the number of days they subscribe to, and daysPresent are the days they took the course.
I am not sure if this is the right schema for my project, so far I was able to do this much.
Confusions with the schema are:
When student who is subscribed to two different courses arrives to the
institute, but takes only one class (course), then I do not think this
schema supports the case, for this I thought to modify courseSchema like this,
var courseSchema = new mongoose.Schema({
name: String,
subsriptionDays: Number,
daysPresent: [Date]
});
But, after doing this I am still confused to make changes on the data, like Date has to be inserted into the documents every time student attends for the course.
Second confusion is how will I update the data inside document every day, only the data that has to be inserted on daily basis is the Date inside days.
Can I get some guidance and suggestion from Mongo Experts? TIA
I think that you are basically on the right track with your second thoughts on extending the design. I would really only expand on that by also including a "reference" to the "Course" itself as opposed to just the information embedded on the schema.
As your usage case questions, then they are probably best addressed with a working example:
const async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/school');
// Course model
const courseSchema = new Schema({
name: String,
info: String
});
const Course = mongoose.model('Course', courseSchema);
// Student Model
const studentCourseSchema = new Schema({
_id: { type: ObjectId, ref: 'Course' },
name: String,
subscriptionDays: Number,
daysPresent: [Date]
});
const studentSchema = new Schema({
name: String,
email: String,
courses: [studentCourseSchema]
});
studentSchema.index({ "email": 1 },{ "unique": true, "background": false });
const Student = mongoose.model('Student', studentSchema);
function logOutput(content) {
console.log( JSON.stringify( content, undefined, 2 ) )
}
async.series(
[
// Clear collections
(callback) =>
async.each(mongoose.models,
(model,callback) => model.remove({},callback),callback),
// Set up data
(callback) =>
async.parallel(
[
(callback) => Course.insertMany(
[
{ "name": "Course 1", "info": "blah blah blah" },
{ "name": "Course 2", "info": "fubble rumble" }
],
callback),
(callback) => Student.insertMany(
[
{ "name": "Bill", "email": "bill#example.com" },
{ "name": "Ted", "email": "ted#example.com" }
],
callback)
],
callback
),
// Give bill both courses
(callback) => {
async.waterfall(
[
(callback) => Course.find().lean().exec(callback),
(courses,callback) => {
courses = courses.map(
course => Object.assign(course,{ subscriptionDays: 5 }));
let ids = courses.map( c => c._id );
Student.findOneAndUpdate(
{ "email": "bill#example.com", "courses._id": { "$nin": ids } },
{ "$push": {
"courses": {
"$each": courses
}
}},
{ "new": true },
(err, student) => {
logOutput(student);
callback(err);
}
)
}
],
callback
)
},
// Attend one of bill's courses
(callback) => Student.findOneAndUpdate(
{ "email": "bill#example.com", "courses.name": 'Course 2' },
{ "$push": { "courses.$.daysPresent": new Date() } },
{ "new": true },
(err, student) => {
logOutput(student);
callback(err);
}
),
// Get Students .populate()
(callback) => Student.find().populate('courses._id')
.exec((err,students) => {
logOutput(students);
callback(err);
}
)
],
(err) => {
if (err) throw err;
mongoose.disconnect();
}
)
So that should give you a sample of how the operations you ask about actually work.
Add a course to the student Shows addition of a couple of courses where I think you would ideally use $push functionality of MongoDB. To ensure that you are not adding courses that are already there the "query" expression actually excludes selection if they are already present in the courses array. In the example a "list" is passed, so we use $nin but with a single item you would simply use $ne:
{ "email": "bill#example.com", "courses._id": { "$nin": ids } },
{ "$push": { "courses": { "$each": courses } } },
Add an attended date This actually demonstrates a case where you would want to "positionally match" the item within "courses" in order to know which one to update. This is done by providing much like before a condition to "match" as opposed to "exclude" the specific array element. Then in the actual "update" part, we apply the same $push operator so we can append to the "daysPresent"array, but also using the positional $ operator to point to the correct array index position which corresponds to the match condition:
{ "email": "bill#example.com", "courses.name": 'Course 2' },
{ "$push": { "courses.$.daysPresent": new Date() } },
As a bonus there are a few more operations in there showing the relational nature between keeping a list of "Courses" in their own collection with additional information that you probably do not want to embed on each student.
The last operation in the sample actually performs a .populate() to actually pull in this information from the other collection for display.
The whole example has debugging turned on with mongoose.set('debug',true); so you can see what the actual calls to MongoDB are really doing for each operation.
Also get acquainted with the .findOneAndUpdate() method used here, as well as the various "update operators" from the core MongoDB documentation.
Sample output
Mongoose: courses.remove({}, {})
Mongoose: students.remove({}, {})
Mongoose: students.ensureIndex({ email: 1 }, { unique: true, background: false })
(node:10544) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
Mongoose: courses.insertMany([ { __v: 0, name: 'Course 1', info: 'blah blah blah', _id: 5944d5bc32c6ae2930174289 }, { __v: 0, name: 'Course 2', info: 'fubble rumble', _id: 5944d5bc32c6ae293017428a } ], null)
Mongoose: students.insertMany([ { __v: 0, name: 'Bill', email: 'bill#example.com', _id: 5944d5bc32c6ae293017428b, courses: [] }, { __v: 0, name: 'Ted', email: 'ted#example.com', _id: 5944d5bc32c6ae293017428c, courses: [] } ], null)
Mongoose: courses.find({}, { fields: {} })
Mongoose: students.findAndModify({ 'courses._id': { '$nin': [ ObjectId("5944d5bc32c6ae2930174289"), ObjectId("5944d5bc32c6ae293017428a") ] }, email: 'bill#example.com' }, [], { '$push': { courses: { '$each': [ { daysPresent: [], _id: ObjectId("5944d5bc32c6ae2930174289"), name: 'Course 1', subscriptionDays: 5 }, { daysPresent: [], _id: ObjectId("5944d5bc32c6ae293017428a"), name: 'Course 2', subscriptionDays: 5 } ] } } }, { new: true, upsert: false, remove: false, fields: {} })
{
"_id": "5944d5bc32c6ae293017428b",
"__v": 0,
"name": "Bill",
"email": "bill#example.com",
"courses": [
{
"subscriptionDays": 5,
"name": "Course 1",
"_id": "5944d5bc32c6ae2930174289",
"daysPresent": []
},
{
"subscriptionDays": 5,
"name": "Course 2",
"_id": "5944d5bc32c6ae293017428a",
"daysPresent": []
}
]
}
Mongoose: students.findAndModify({ 'courses.name': 'Course 2', email: 'bill#example.com' }, [], { '$push': { 'courses.$.daysPresent': new Date("Sat, 17 Jun 2017 07:09:48 GMT") } }, { new: true, upsert: false, remove: false, fields: {} })
{
"_id": "5944d5bc32c6ae293017428b",
"__v": 0,
"name": "Bill",
"email": "bill#example.com",
"courses": [
{
"subscriptionDays": 5,
"name": "Course 1",
"_id": "5944d5bc32c6ae2930174289",
"daysPresent": []
},
{
"subscriptionDays": 5,
"name": "Course 2",
"_id": "5944d5bc32c6ae293017428a",
"daysPresent": [
"2017-06-17T07:09:48.662Z"
]
}
]
}
Mongoose: students.find({}, { fields: {} })
Mongoose: courses.find({ _id: { '$in': [ ObjectId("5944d5bc32c6ae2930174289"), ObjectId("5944d5bc32c6ae293017428a") ] } }, { fields: {} })
[
{
"_id": "5944d5bc32c6ae293017428b",
"__v": 0,
"name": "Bill",
"email": "bill#example.com",
"courses": [
{
"subscriptionDays": 5,
"name": "Course 1",
"_id": {
"_id": "5944d5bc32c6ae2930174289",
"__v": 0,
"name": "Course 1",
"info": "blah blah blah"
},
"daysPresent": []
},
{
"subscriptionDays": 5,
"name": "Course 2",
"_id": {
"_id": "5944d5bc32c6ae293017428a",
"__v": 0,
"name": "Course 2",
"info": "fubble rumble"
},
"daysPresent": [
"2017-06-17T07:09:48.662Z"
]
}
]
},
{
"_id": "5944d5bc32c6ae293017428c",
"__v": 0,
"name": "Ted",
"email": "ted#example.com",
"courses": []
}
]
Schema you can define like:-
var mongoose = require('mongoose');
var courseSchema = new mongoose.Schema({
name: String
});
var studentSchema = new mongoose.Schema({
name: String,
email: { type: String, unique: true},
phone: String,
gender: String,
age: String,
city: String,
street: String,
picture: String,
courses: [{
course:{type:mongoose.Schema.Types.ObjectId,ref:'courseSchema'},
isAttending:{type:Boolean ,default:false}
}],
subscriptionDays: Number,
daysPresent: [Date]
}, schemaOptions);
module.exports = mongoose.model('Student', studentSchema);
isAttending will solve your problem if studen subscribe 3 courses and going to particular one course then isAttending will be true otherwise false.
You Can use Cron npm module which will run a function on what time will you set and its make your life easy.
Thanks