Find one and insert into child referenced document - mongodb

I have a User model, with a referenced child array of objects, I would like to find the User, and insert into the child referenced document, I don't want to update the child document, but rather insert another object into the reports document array.
Below is the User model, I basically want to find the User, and insert into Reports.
const User = mongoose.model(
"User",
new mongoose.Schema({
firstName: String,
lastName: String,
dateOfBirth: String,
email: String,
password: String,
agreedToTerms: Boolean,
agreementDate: String,
verified: Boolean,
roles: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Role"
}
],
reports: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Reports"
}
]
})
);
Thank you!

You can use findByIdAndUpdate with $push:
User.findByIdAndUpdate(<user-id>, {
$push: { reports: <report-id> }
});

Related

How to navigate through documents in a circuler linked list fashion in MongoDB?

I have a really simple User model.
const userSchema = mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
address: {
type: String,
default: null,
},
description: {
type: String,
default: null,
},
active: {
type: Boolean,
default: true,
},
interests: [
{
type: String,
default: null,
},
],
favorites: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
default: null,
},
],
});
I have a use-case where the client should be able to navigate through the users one by one in a circular fashion. I was able to implement the functionality for getting the next and previous user relative to a specific user through the
this answer.
However, the issue with this is that if I try to get the previous document on the first document in the Users collection, it of course returns an empty object. And same is the case when I try to get the next document on the last document.
I want to tweak it so that if I try to get the previous document on the first document, it would return me the last document, and if I try to get the next document on the last document, it would return me the first document. Just like how you traverse through a circular linked list. Any help would be appreciated! Thanks.

Moongose: .populate on key with white space doesn't work

I'm using Express and Mongoose to do some database actions. When trying to populate a path where the key includes a whitespace, it basically get's ignored.
MongoDB Model:
const OrgCrimeSchema = new Schema({
gracePeriod: { type: Date, default: Date.now() },
Technical: {
description: String,
difficulty: Number,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
},
'Social Engineering': { // This one causes issues
description: String,
difficulty: Number,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
},
});
Find + Populate:
const now = Date.now();
const allOrgCrimes = await OrgCrime.find({ gracePeriod: { $lte: now } })
.populate('Technical.owner', 'name')
.populate('Social Engineering.owner', 'name');
console.log(allOrgCrimes['Social Engineering'][0].owner);
//5fca3b4a86e77b5c8e58b683
console.log(allOrgCrimes['Technical'][0].owner);
// { name: 'npc_alice', _id: 5fae6d7ee60018434108369c }
I assume the path is not being populated because of a white space in the key. I've tried both dot notation and typing {path: 'Social Engineering', select: 'name -id'}, without luck.
Is there any way around this without having to rewrite the schema structure?
In short, there is no way, if we wanted to populate multiple paths at the same time there is this way
for exmaple
Story
.find(...)
.populate('book author') // space delimited path names
.exec()
you can see space delimited path names.
When you pass a key with space,mongoose consider it as populate multiple
As poined out by Mohammad Yaser Ahmadi, there is no solution to this issue today because of populate assumes you're populating multiple instead of trying to read a path name with a space in between. How I 'solved' this was to rewrite the model to this:
const OrgCrimeSchema = new Schema({
gracePeriod: { type: Date, default: Date.now() },
roles: [{
role: String,
description: String,
difficulty: Number,
owner: { type: Schema.Types.ObjectId, ref: 'User' },
}]
})
And then the populate actually get's easier:
.populate('roles.owner', 'name')

Mongoose: reference between models with same schema keys

As a front-end developer, I would like to have some isomorphic object for two mongoose models.
Let's say I have a user profile:
const profileSchema = new Schema({
firstName: { type: String },
lastName: { type: String },
// example of difference between model schemas
__user: { type: ObjectId, ref: 'Users' },
}
And I would like to create a list of Contact, where each contact will have some of the same keys:
const contactSchema = new Schema({
firstName: { type: String },
lastName: { type: String },
__profile: {
type: ObjectId,
ref: 'Profiles',
unique: true,
},
comment: { type: String },
}
NOTE: Contact could be the both:
as a reference to the Profile
and as independent record in DB / document.
==============================
My question: which is the best way to, organize models on such a way, so
contact could be a reference to the profile
when similar Profile key, like firstName will be updated, contact firstName will be updated too
AVOID of next ref
await Contact.findById(SOME_ID).populate('__profile');
// result
{
firstName: '',
lastName: '',
__profile: {
firstName: 'Chuck',
lastName: 'Norris',
}
}
Desired result - keep contact "isomorphic" like:
{
firstName: 'Chuck', // the key value from profile
lastName: 'Norris', // the key value from profile
__profile: SOME_PROFILE_ID,
}
Is this possible?
P.S: in my app, I'm using refs and started to use discriminators approaches.
I'd approach this task either:
1) put all data inside 1 collection (e.g. Profile):
// Profile model
{
firstName: 'Chuck',
lastName: 'Norris',
contacts: [{
type: ObjectId,
ref: 'Profile',
unique: true,
}],
...all other properties
}
that way you will be able to store just contacts (e.g. when I want to add just a contact) and profiles with much more info.
2) or will use discriminators to create a base class (e.g. Contact) and build Profile model upon it:
const options = { discriminatorKey: 'userType' };
const Contact = mongoose.model('Contact', new mongoose.Schema({
firstName: String,
lastName: String
},
options)
)
const Profile = Contact.discriminator(
'Profile',
new mongoose.Schema(
{
contacts: [{
type: ObjectId,
ref: 'Contact',
unique: true,
}],
comments: []
},
options
)
);
that way you will be able to save Contacts and Profiles inside 1 collection and reference base class (Contact) for contacts inside Profile
Hope that helps!
In my case, complete usage of Mongoose discriminators did not give me an advantage, because discriminators give you an opportunity to:
They enable you to have multiple models with overlapping schemas on
top of the same underlying MongoDB collection.
As a result, by using discriminators approach, I will reive one collection
of:
profiles
And there will be a mix of users and contact profiles.
==============================
So I decided to use two approaches:
create BesaSchema for the profile
take advantage of Mongoose Subdocuments
RESULT:
// keys which are same for both user Profile and Contact
const Schema = require('mongoose').Schema;
const util = require('util');
function BaseProfileSchema(...args) {
Schema.apply(this, args);
this.add({
firstName: { type: String },
lastName: { type: String },
});
}
util.inherits(BaseProfileSchema, Schema);
// user Profile Model
const profileSchema = new BaseProfileSchema({
__user: {
type: String,
ref: 'users',
required: true,
unique: true,
},
});
const Profile = mongoose.model('profiles', profileSchema);
// Contact with profile as subdocument
const contactProfileSchema = new BaseProfileSchema();
const contactSchema = new Schema({
// Associations
__refProfile: {
type: Schema.Types.ObjectId,
ref: 'profiles',
index: {
unique: true,
sparse: true,
},
},
profile: contactProfileSchema,
});
const Contact = mongoose.model('contacts', contactSchema);
As a result, I'm having DB with next collections:
users
profiles
contacts
Both profiles and contacts.profile are IDENTICAL because I'm extending the base shared schema.
Moreover:
inside Contact I'm having different keys for real referenced profile (__refProfile which can NOT be edit by others) and contact.profile
profile inside connection can be edited ONLY when the contact was edited by itself
P.S: happy codding 👨‍💻🍻

how to database design for user authentication like student, teacher , super admin

I am trying to create different types of registration for user . I have got three collection for users . I have been references user collection in both of teacher and student because I need to get email and password.
If a teacher register including email, password, firstname , lastname etc , there is a collection .
if a student register including email, password, firstname , lastname etc , there is another collection .
our all of email and password will be one collections
user - table/collection
- email : test#gmail.com
- password: asdfasdf
student - table /collection
- firstname: 'sujon"
teacher - table/collection
- firstname: "sujon"
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
})
const StudentSchema = mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user',
},
firstname: {
type: String,
},
lastname: {
type: String,
},
photo: {
type: String,
},
education: {
type: String,
},
birth: {
type: Date,
},
sex: {
type: Boolean,
},
})
const TeacherSchema = mongoose.Schema({
user: {
type: mongoose.Schema.ObjectId,
ref: "user"
},
firstname: {
type: String
},
lastname: {
type: String
},
photo: {
type: String
},
designation: {
type: String
},
birth: {
type: Date
},
sex: {
type: Boolean
}
});
how can implement database design
Creating a single User schema would be fine. You can have a single schema with all properties (since all three types of user have almost same properties) and then add a 'roles' field like this:
roles: [{ type: String }]
The roles field can have multiple roles [ 'Teacher', 'Student' ... ]. In this way a single user can have multiple roles for example a Teacher can also be an admin etc.
Also you won't have to create a new model whenever a new role is introduced.
Users can be queried based on their roles. The roles can then be used for authentication as well for example a user with role 'Teacher' can create assignment or a user with role 'Student' can submit assignments. When registering a user you can set some sort of model validation on the api or client side to accept a certain model.

Waterline: Create or update a populate record

My question is basic but I can not find an answer in the documentation. I have two models: 'Person' and 'Location' being associated One-to-one. I want to create or update 'populated child records' in the Location collection.
let person = await Person.findOne({ phone: inputs.phone }).populate('reside');
Well return
{ reside: [],
createdAt: 1540081110195,
updatedAt: 1540331824622,
id: '5bcbc5d609618e2cfbe64883',
phone: '+42424242',}
I want to create new location record (when they do not exist) or update if their exist. I try
let location = await Location.update({ id: person.reside.id })
.set({city: inputs.city,
street: inputs.street,
}).fetch;
But it does not work when no record has been created yet.
model/Person.js
phone: {
type: 'string',
unique: true,
},
reside: {
collection:'location',
via: 'owner'
},
models/Location.js
city: { type: 'string' },
street: { type: 'string' },
owner:{
model:'person',
unique: true
}
I use action2
I finally found here methods .addToCollection(), .removeFromCollection(), or .replaceCollection() to modify the populated values of a particular record or set of records. It does not seem to be the most appropriate place in the doc to talk about it