Skip to content

Tag: mongodb

How do I use $addToSet with objects in mongodb?

I want to push only unique objects into an array with $addToSet. My userSchema has a field bankDetails which should take an array of objects: I am updating the schema using the following controller function: The data is saving correctly but Mongo auto generates an _id field every time so technically none of t…

Data Format, how to map this object?

I’d like to map out an array from a backend API call. This comes from a schema which currently has the format of Within this, I have an object, however it looks like this for example: Given the above, i’d like to map through this data on a component. I have tried to store it into a const, then map

How to add new value with same key to document

I need to write down a category and its cost in category object. Now, in my code example, I am adding the products: $50 category to the user. After execution, ‘categories’: { products: ’50$’ } is displayed in the console. But if you add products: 1000$ again, it will overwrite and outp…

Sort the array of database object-id

I have a array of mongodb object-id and i want to sort them in such a way that similar IDs are next to each other example : Input : var array = [“507f191e810c19729de860ea”,”00000020f51bb4362eee2a4d”,” 507f191e810c19729de860ea”] Output : var array = [“507f191e810c19729de860ea&#822…

MongoDB attribute to array migration script

I’m trying to make a migration for a database, which has the following structure as of now: There have been some changes in how we need to handle a site, having now the possibility to have multiple sites, and not only one. So I need to change the DB to look like this: Is there a way to change the