title: How to retrieve an array inside a node in mongoDB using node.js?
body: <p>I have a mongoDB collection called 'users'. Inside the 'users' collection there are several users. Each user has an array called Cart and there are items inside the cart like this,</p>
  
  <p><a href="https://i.stack.imgur.com/izgj3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/izgj3.png" alt="enter image description here"></a></p>
  
  <p>Now I want to retrieve all the 'Cart' details (or objects) of a user by giving the user's id (_id).
  I tried in this way but it throws an exception</p>
  
  <pre><code>router.get('/getCart', (req, res, next) =&gt; {
  
      console.log(req.body.userId)
      User.findOne({_id: req.body.userId}
      ,(err, userInfo) =&gt; {
          userInfo.Cart.find({}, (err, result) =&gt; {
              if (err) return next(err);
  
              let cart = {
                  status: 'success',
                  code: 200,
                  data : result
              };
  
              res.json(cart);
          })
      })
  });
  </code></pre>
  
  <p>The exception</p>
  
  <pre><code>TypeError: #&lt;Object&gt; is not a function
      at CoreMongooseArray.find (&lt;anonymous&gt;)
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/src/routes/cart.js:58:23
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/model.js:4837:16
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/query.js:4391:12
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/query.js:2869:28
      at processTicksAndRejections (internal/process/task_queues.js:79:11)
  Emitted 'error' event on Function instance at:
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/model.js:4839:13
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/query.js:4391:12
      at /Users/applefactory/WebstormProjects/OnlineFashionStore/src/UserBackend/node_modules/mongoose/lib/query.js:2869:28
      at processTicksAndRejections (internal/process/task_queues.js:79:11)
  </code></pre>
  
  <p>How can I solve this issue? Or is there any other way achieve this? Thanks in advance!</p>
  
  <pre><code>   ------------------------------ UPDATE --------------------------------------
  </code></pre>
  
  <p>This is my User model</p>
  
  <pre><code>const UserSchema = new mongoose.Schema({
      username: {
          type: String,
          validate: {
              validator: username =&gt; User.doesNotExist({username}),
              message: "Username already exists"
          }
      },
  
      email: {
          type: String,
          validate: {
              validator: email =&gt; User.doesNotExist({email}),
              message: "Email already exists"
          }
      },
  
      password: {
          type: String,
          required: true
      },
  
      Cart : {
          type: Array,
          default: []
      },
  
      WishList : {
          type : Array,
          default: []
      },
  },{timestamps: true});
  
      UserSchema.pre('save', function () {
          if(this.isModified('password')){
              this.password = hashSync(this.password, 10);
          }
      });
  
      UserSchema.statics.doesNotExist = async function (field) {
          return await this.where(field).countDocuments() === 0;
      };
  
      UserSchema.methods.comparePasswords = function (password) {
          return compareSync(password, this.password);
      };
  
      const User = mongoose.model("User", UserSchema);
      export default User;
  </code></pre>