Skip to content
Advertisement

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 the objects are unique and the objects

RegExp not working in prisma aggregateRaw for MongoDB

I’m new to mongo and prisma in javascript. This is a mongo query using prisma’s aggregateRaw method. The cond should do a case-insensitive string comparison. For example if $$property.property_name is “The Brough”, a regex /the br/i should be true. Prisma throws an error it doesn’t like the RegExp or /regex/ notation. This same query works from mongosh command line though.

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 output ‘categories’: { products: ‘1000$’ }. And I need

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”,”507f191e810c19729de860ea”,”00000020f51bb4362eee2a4d”] Answer Simply sorting the input array would result in the same input array being sorted in natural order (converting the

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

Advertisement