Search code examples
mongodbmongoose

How to extract the field that is not equal to a set of values in Mongodb Mongoose Nodejs


I am extracting the document where its status is not equal to Finished, the below code works well.

const x = await rmm.Events.find(
    { status: { $ne: "Finished" } },
    { startTime: true, name: true, _id: false }
  );

However, if I want to extract the status is not equal to Finished or Voided, the below code failed.

const x = await rmm.Events.find(
    { status: { $ne: ["Finished", "Voided"] } },
    { startTime: true, name: true, _id: false }
  );

How should I fix it?


Solution

  • you have to use $nin instead

    const x = await rmm.Events.find(
        { status: { $nin: ["Finished", "Voided"] } },
        { startTime: true, name: true, _id: false }
      );
    

    playground