{ _id: ObjectId("62541083e601e0bddf0e1681"),
name: 'Monitioir',
brand: 'sonic',
price: 70000,
category: 'monitor',
user_id: '62541073e601e0bddf0e167d',
__v: 0 }
{ _id: ObjectId("62540d527fe95cb11db5ad86"),
name: 'Laptop 8 GB Ram',
brand: 'DEL',
price: 60000,
category: 'laptop',
user_id: '62517edb7fc85c423a5b12c3',
__v: 0 }
I have a 2 products in my collection inserted by 2 different users in front-end I am creating a search bar where user can search only the products added by him only the search api that I have created is also returning the products added by the other users also my query is
db.products.find({$or:[{name:{$regex:"o"}}]})
after executing this query both users products are coming i want only of specific user.
Related
If this is my db collection
{
_id: ObjectId("*********"),
name: 'xyz',
owner: 'abc',
Shop Number: 56,
stock: 43
}
and i want to update it with area field so how to write query to find the collection and update it with area
I have a collection of users and a document can be as follows...
{
_id: ObjectId('this user id'),
firstName: 'first name of this user',
lastName: 'last name of this user',
tvs: [
{
_id: ObjectId('this tv id'),
brand: 'This tv brand',
size: 'this tv size',
},
{
_id: ObjectId('this other tv id'),
brand: 'this other tv brand',
size: 'this other tv size',
}
]
}
I am having trouble trying to update a document embedded in the array of tvs using only req.body. I managed to be able to update the document based on the tv id by doing the following...
await User.updateOne(
{
_id: req.user.id,
'tvs._id': req.params.id,
},
{
$set: { 'tvs.$.brand': req.body.brand },
}
);
but what I want to do is something like this...
await User.updateOne(
{
_id: req.user.id,
'tvs._id': req.params.id,
},
{
$set: { 'tvs.$': req.body },
}
);
where req.body can have many fields and will only update the fields specified and leave the other ones untouched. Problem is that when I do this it changes my element to have only the id and whatever fields I may have in req.body and gets rid of all the other fields.
Is there a performance difference if you have an array within a data set (tags) like this:
images cluster:
{
_id: 'imageID1',
title: 'some title name',
tags: ['cool', 'banana', 'animal']
},
{
_id: 'imageID2',
title: 'some other title name',
tags: ['funny', 'creative', 'animal']
}
vs separating the array into different clusters? (I'm using mongodb as my database)
images cluster:
{
_id: 'imageID1',
title: 'some title name',
tagsId: 'imageID1tags'
},
{
_id: 'imageID2',
title: 'some other title name',
tagsId: 'imageID2tags'
}
Tags cluster:
{
_id: '1',
imagesId: 'imageID1',
tag: 'cool'
},
{
_id: '2',
imagesId: 'imageID1',
tag: 'banana'
},
{
_id: '3',
imagesId: 'imageID1',
tag: 'animal'
},
{
_id: '4',
imagesId: 'imageID2',
tag: 'funny'
},
{
_id: '5',
imagesId: 'imageID2',
tag: 'creative'
},
{
_id: '6',
imagesId: 'imageID2',
tag: 'animal'
},
Just side note: You can see (for example purposees), I have 2 animal data, where 1 of them belongs to imageID1 and another imageID2. This cluster will have endless amounts of tags, where it can be duplicate names, unique to its imagesId.
So does it make sense to just simplify it (for scalability and performance considerations) and have it within the images cluster (just 1 data cluster), or spread it out (having 2 data clusters)? I want to eventually do a search on the tags, and then the images associated to the search tag will populate.
So if user searches animal, both of the images will show up.
I want to create an expense tracking app but don't know how to make my schema structure.
The layout:
The schema:
module.exports = {
expenseList: [
{
uid: 'some-uid1',
createdAt: 'some-date1',
expenseItems: [
{
date: 'date11',
desc: 'desc11',
amount: 'amount11'
},
{
date: 'date11',
desc: 'desc11',
amount: 'amount11'
}]
},
{
uid: 'some-uid',
createdAt: 'some-date',
expenseItems: [
{
date: 'date1',
desc: 'desc1',
amount: 'amount1'
},
{
date: 'date1',
desc: 'desc1',
amount: 'amount1'
},
{
date: 'date1',
desc: 'desc1',
amount: 'amount1'
}]
}
]
};
How do I change my schema so I can loop through expenses and display createdAt property as a clickable link that redirects to that expense items?
When do I use Arrays and when do I use Objects in my schema?
Where do I need uid (unique ids)?
I will use firebase for this app
You have an expenseList with a date and expenseItems associated with it. You can have a schema roughly like this. expenseList will be an array of objects. Each expenseList is an object containing uid (to uniquely identify the list), createdAt and array of expenseItems with its own set of properties.
expenseList: [{
uid: 'some-uid',
createdAt: 'some-date',
expenseItems: [
{
date: 'date1',
desc: 'desc1',
amount: 'amount1'
},
{
date: 'date1',
desc: 'desc1',
amount: 'amount1'
}
]
}]
You can iterate over this expenseList to display createdAt in your first screen. On clicking of that, depending on the unique id of that expenseList, fetch the expenseList and display the expenseItems on the second screen.
I have not used firebase before, so you might have to tweak the schema a little.
We have 3 entities.
Article
Note
Comment
Article can have 0 or many Notes and Note can have 0 or many Comments
Our actions
1. We want to fetch from db single Note by Note id
2. We want to add new Note to Article
3. We want to add new comment to Note
Scheme 1
{
_id: 'article_1',
name: "Article 1",
notes: [{
_id: 'note_1',
name: 'Note 1',
comments: [{
content: 'Hello World'
}]
}]
}
Approach 1
1. db.Article.find({_id: 'article_1', 'notes._id': 'note_1'}, {'notes.$': 1})
2. db.Article.update({_id: 'article_1'}, {$push: {notes: {_id: 'note_2', name: 'note2'}}})
3. db.Article.update({_id: 'article_1', 'notes._id': 'note_2'}, {$push: {'notes.$.comments': {content: 'Hi !!!'}}})
Scheme 2
{
_id: 'article_2',
name: "Article 2",
notes: {
'note_1': {
name: 'Note 1',
comments: [{
content: 'Hello World'
}]
}
}
}
Approach 2
1. db.Article.find({_id: 'article_2'}, {'notes.note_1':1})
2. db.Article.update({_id: 'article_2'}, {$set: {'notes.note_2': {name: 'note2'}}})
3. db.Article.update({_id: 'article_2'}, {$push: {'notes.note_2.comments': {content: 'Hi !!!'}}})
Which scheme and approach is considered to be best practice and which for better performace and why.
Thank you