MongoDB - will the collection referencing another collection be also updated when referenced collection updates and vice versa? - mongodb

If I update a field in collection B that is referenced by collection A, will collection A update too? Also vice versa?
collection A:
const userSchema = new mongoose.Schema({
email: String,
userName: String, //lets say I update username
password: String,
favoriteBooks: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Book'
}
})
collection B:
const bookSchema = new mongoose.Schema({
name: String,
reviews: [String] //lets say I update or add review
})
If I update a subdocument:
collection A:
const userSchema = new mongoose.Schema({
email: String,
userName: String, //lets say I update username
password: String,
favoriteBooks: [BookSchema]
})
collection B:
const bookSchema = new mongoose.Schema({
name: String,
likesCount: Number
})
Thanks

Related

MongoDB Reference ObjectID from the same table

I want to do a category table that have a parent category by referencing the ObjectId from the same table.
Is it a normal practice or bad practice to refer ObjectId from the same table? If this is wrong, how do I go about it?
const CategorySchema = new Schema({
title: String,
description: String,
thumbnail: String,
image: String,
parentCategory: {
type: Schema.Types.ObjectId,
ref: 'category',
},
})
module.exports = mongoose.model('category', CategorySchema)

differentiating Objects with same value in a collection in mongodb

I have a model named author:
export const authorInfoSchema = new Schema({
a_id: String,
firstName: String,
lastName: String,
birthDay: String,
});
for each author I want to add objects as a Schema named books:
const bookSchema = new Schema({
book_id: String,
bookName: String,
bookDis: String,
bookDate: Date,
bookAuthor: String,
pageNo: Number,
});
but there could be a case where 2 or more authors have the same name, how can I differentiate between the authors and link them to their right books?

Cannot access some properties from Mongoose document

I am doing a query on my database using Mongoose to retrieve all documents in a collection. Currently there is only one document in the collection. It returns the document and looks fine, but I cannot access some of the properties.
Code snippet:
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
}).catch((err)=>{console.log(err)});
Result:
[
{
_id: 5f6d4dc312c76000170c5c43,
name: 'Bob',
email: 'bob#mail.com',
zipCode: '12345',
pageVisited: 'p1m2549',
dateTime: 2020-09-25T01:54:11.152Z,
ipAddr: '111.111.111.111',
__v: 0
}
]
5f6d4dc312c76000170c5c43
Bob
bob#mail.com
undefined
undefined
undefined
undefined
What could be causing this bizarre behavior? It really doesn't make any sense that I can access some of the properties but not others.
That could be because these elements not be defined in the Schema
Define Schema as mentioned below
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: String,
email: String,
zipCode: String,
pageVisited: String,
dateTime: Date,
ipAddr: String,
__v: Number
});
var User = mongoose.model('users', UserSchema );
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
console.log(response[0].__v);
}).catch((err)=>{console.log(err)});

Mongodb Schema using mongoose

This is my user schema :
const userSchema = new Schema({
email: String,
username: String,
password: String,
secretToken: String,
active: Boolean,
type: String
}, {
timestamps: { // this will give us the detail when the account is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
this is my requirement Schema:
const requirementSchema = new Schema({
name: String,
age: String,
class: String,
subject: String,
email: String
}, {
timestamps: { // this will give us the detail when the requiremnt is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
how can I bring id from user schema to the requirement schema as secondary key?
I think mongodb is not meant for relationship. I could led to very bad practice if your database grow. But you can try https://docs.mongodb.com/manual/reference/database-references/#DatabaseReferences-DBRef for Database References.
There is no primary key foreign key concept in Mongodb. and normalization is also discouraged in mongodb.

Populate new field when query documents

I have two schemas (using mongoose) like below:
var PostSchema = new Schema({
name: String,
content: String,
likes: [{
type: String,
ref: 'User'
}],
...
})
var UserSchema = new Schema({
name: String,
pinnedPosts: [{
type: String,
ref: 'Post'
}],
...
})
Now I want to get all posts with two new populated fields: isLiked and isPinned depend on the auth state. If the user hasn't signed in (auth is null) then all these two fields are false. If user has signed in and had liked the post, isLiked is true... Can I do it when I query the post?