How to find city document by matching state _id

How to find document by Object Id in atlas function.

try {
    // Execute a FindOne in MongoDB 
    let id = query.city_id;
    let o_id = new BSON.ObjectId(query.city_id)
    findResult = await collection.find({ "_id": o_id });

  } catch(err) {
    console.log("Error occurred while executing findOne:", err.message);

    return { error: err.message };

I am getting state id in query and I want to get matching city, with other key I am getting state but with object id I am not getting any state


Three quick thoughts looking at your function:

  1. Is your function defined as an “async” function? If not, then using await is not supported. See here for a sample:
  2. What is the “type” of query.city_id? Is it already an ObjectId? If so, there is no need to wrap it in an ObjectId()
  3. You can switch from using Find() (which returns a cursor since there might be multiple results), to a FindOne() (which returns the single item you are querying for by _id)

Let me know if any of these suggestions help,