1

I want to retrieve based on their list of IDs given. There is a list of ids I used inside my controller I want to retrieve all the objects array based on the given list of ids to me here is a prototype of my database record how it looks like

[
  {
    "_id": ObjectId("630a2e258bb6b10016ca68f1"),
        "deviceId": "42925f6a6eec14d8",
        "personId": ObjectId("630a2e218bb6b10016ca68eb"),
        "createdAt": 1661611557637,
        "updatedAt": 1661611557637,
    },
  {
    "_id": ObjectId("630a2e258bb6b10016ca68f1"),
        "deviceId": "42925f6a6eec14d8",
        "personId": ObjectId("630a2e218bb6b10016ca68eb"),
        "createdAt": 1661611557637,
        "updatedAt": 1661611557637,
    },
  {
    "_id": ObjectId("630a2e258bb6b10016ca68f1"),
        "deviceId": "42925f6a6eec14d8",
        "personId": ObjectId("630a2e218bb6b10016ca68eb"),
        "createdAt": 1661611557637,
        "updatedAt": 1661611557637,
    },
  {
    "_id": ObjectId("630a2e258bb6b10016ca68f1"),
        "deviceId": "42925f6a6eec14d8",
        "personId": ObjectId("630a2e218bb6b10016ca68eb"),
        "createdAt": 1661611557637,
        "updatedAt": 1661611557637,
    }
]

Here is what i am trying to do:

getData: async function (req, res) {

    // var db = Device.getDatastore().manager;

    let ObjectId = require("mongodb").ObjectID;

    let usersID = [
      "62f79104bb4b3d0016260b88",
      "62f925a3bcbc910016a360b6",
      "630a2e218bb6b10016ca68eb",
    ];

    var devices = await Device.find({
      personId: {
        $in: [...usersID],
      },
    });
    if (!devices) {
      return res.badRequest("Please specify search criteria");
      // var devices = await Device.find();
    }
    return res.successResponse(
      devices,
      200,
      null,
      true,
      "${devices.size()} roles are found."
    );
  },
Developer Nans
  • 311
  • 1
  • 4
  • 14
  • you need to convert string ids to object id by `let usersID = [ObjectId("62f79104bb4b3d0016260b88"), ObjectId("62f925a3bcbc910016a360b6"), ObjectId("630a2e218bb6b10016ca68eb")];` ` – turivishal Nov 23 '22 at 15:20
  • Not sure why are you spreading the array, just to add it another array? You can just do `$in : usersID` and of course you will have to convert to ObjectID as @turivishal mentioned. You can run a `.map` to convert all of them in one loop. In ideal world ID's should already be in ObjectID format – Shivam Nov 23 '22 at 15:22
  • @Shivam I tried both of your suggestion its giving the same error `"Could not use the provided `where` clause. Could not filter by `personId`: Unrecognized modifier (`$in`) within provided constraint for `personId`."` – Developer Nans Nov 23 '22 at 15:25

1 Answers1

0

You could use aggregate to change de objectId to string and get the result you want.

here is an example:

 const devices = await Device.aggregate([
  {
    "$addFields": {
      "personId": {
        "$toString": "$personId"
      }
    }
  },
  {
    "$match": {
      "personId": {
        $in: [
          "62f79104bb4b3d0016260b88",
          "62f925a3bcbc910016a360b6",
          "630a2e218bb6b10016ca68eb"
        ]
      }
    }
  }
])

Or:

 const devices = await Device.find(
  {"personId": {
        $in: [
          ObjectId("62f79104bb4b3d0016260b88"),
          ObjectId("62f925a3bcbc910016a360b6"),
          ObjectId("630a2e218bb6b10016ca68eb")
        ]
      }
    }
).toArray();
Martinez
  • 1,109
  • 1
  • 4
  • 13
  • Thanks for the answer but its not working the result is empty object – Developer Nans Nov 23 '22 at 15:29
  • hmm, could you share how are you doing it, because I test it and work here https://mongoplayground.net/p/6N2sX_HVI5I – Martinez Nov 23 '22 at 15:31
  • is there any other way to get the data I wanted without adding another field – Developer Nans Nov 23 '22 at 15:37
  • you got personId as ObjectId to match with the array of string you must convert the objectId to string or the string to objectId, but if your problem it the personIdSTR field you could remplace it with personId. I will edit my answer to show you how to achieve it. – Martinez Nov 23 '22 at 15:42
  • Yes thanks I tried in the playground it works but some how its not working in my code – Developer Nans Nov 23 '22 at 15:44
  • Make sure you have record in your device collection with the personId you are providing, another thing it that you got duplicated _id in your record example. – Martinez Nov 23 '22 at 15:47
  • No, I didn't give the total data i just a few of the fields and copied them multiple times pasted them. Actually, there are 18 fields that will confuse others so I remove a few fields from the question – Developer Nans Nov 23 '22 at 15:54