Afdal
Afdal

Reputation: 655

How to modify data from mongodb?

How to modify data from mongodb?

I have data from mongobd by query:

ProfileModel.find(
            {
                _id: {
                    $in: [
                        '63a0488c88723874c1fb3fbf',
                        '63a04894d4bdd0a191b69573',
                    ]
                }
            }
        )

Original Data From MongoDB:

[
 {
  name: "Name",
  image: "https//image......"
 },
 {
  name: "Name",
  image: "https//image......"
 }
]

I want to modify Original Data From MongoDB like this:

data: [
 {
   type: "User",
   attributes: {
     name: "Name",
     image: "https//image......"
   }
 },
 {
   type: "User",
   attributes: {
     name: "Name",
     image: "https//image......"
   }
 }
]

Upvotes: 0

Views: 32

Answers (1)

node_modules
node_modules

Reputation: 4925

Not an entire clean solution, but this will get you on the right way. You could use Array.prototype.map() function to create a new array using the original array you received from your database, see the following snippet:

const someDataArray = [
 {
  name: "Name",
  image: "https//image......"
 },
 {
  name: "Name",
  image: "https//image......"
 }
];

const newArray = someDataArray.map(item => {
  return {
    type: "User",
    attributes: {
      name: item.name,
      image: item.image
    }
  };
});
console.log({ data: newArray });

Upvotes: 1

Related Questions