javascriptarraysresponsemongoose-schemamongoose-populate

why does _doc appear after I use map in result mongoose


I try to using map to my result

ProductModel.find()
.sort({ _id: -1 })
.then((products) => {

    console.log(products)

    products = products.map((product) => {
      return { ...product, price: toIdr(product.price) };
    });

    console.log(products)

    res.render("index", {
      title: "Home",
      products,
      path: req.path,
    });
  });

The first log:

[
  {
    _id: new ObjectId('65ffe52c31f86de770a5110c'),
    title: 'naruto',
    price: 32000,
    description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
    imageUrl: '/assets/book1.jpg',
    userId: new ObjectId('65ffe505f94c639905f4ba13'),
    __v: 0
  }
]

Then the second log, after using map in result array, show me a bunch of value I didn't know

[
  {
    '$__': InternalCache { activePaths: [StateMachine], skipId: true },
    '$isNew': false,
    _doc: {
      _id: new ObjectId('65ffe52c31f86de770a5110c'),
      title: 'naruto',
      price: 32000,
      description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
      imageUrl: '/assets/book1.jpg',
      userId: new ObjectId('65ffe505f94c639905f4ba13'),
      __v: 0
    },
    price: 'Rp 32.000,00'
  }
]

I'm trying to format the price of each product to the new array, so why I cannot just get value like this in second log

[
  {
    _id: new ObjectId('65ffe52c31f86de770a5110c'),
    title: 'naruto',
    price: 'Rp 32.000,00',
    description: 'Vel corporis laboriosam. Fugit sed unde nulla nesciunt laudantium.',
    imageUrl: '/assets/book1.jpg',
    userId: new ObjectId('65ffe505f94c639905f4ba13'),
    __v: 0
  }
]

Solution

  • The spread operator(...) is copying not only the properties of the document but also some internal properties that Mongoose uses to manage the document's state. you can use the lean() method before the find() query. It tells Mongoose to return plain JavaScript objects instead of Mongoose documents.