1

This is the query to select the room document that contains user1 and user2.

db.getCollection("rooms").find({
$and:[            
    {users: ObjectId("5e2916615f55cc6e3cb9838b")},    
    {users: ObjectId("5e2b51107d1c624260620e9e")}    
   ]
})

It works well in mongodb shell and returns following documents.

{ 
    "_id" : ObjectId("5e55ae0e07f8bf48dc602d1c"), 
    "users" : [
        ObjectId("5e2916615f55cc6e3cb9838b"), 
        ObjectId("5e2b51107d1c624260620e9e")
    ], 
    "label" : "", 
    "__v" : NumberInt(0)
}
{ 
    "_id" : ObjectId("5e55ae98bec6265a48d453e6"), 
    "users" : [
        ObjectId("5e2916615f55cc6e3cb9838b"), 
        ObjectId("5e2b51107d1c624260620e9e")
    ], 
    "label" : "", 
    "__v" : NumberInt(0)
}

I want to use this query in nodejs with mongoose.

import { Schema, model } from "mongoose";
import mongodb from "mongodb";

const RoomSchema = new Schema({
  users: [
    {
      type: Schema.Types.ObjectId,
      ref: "User",
      required: true,
      autopopulate: { select: "name phone photo" }
    }
  ],
  label: { type: String, text: true }
});

export default model("Room", RoomSchema);
... ... ...
const filter = {
        $and: [
          { users: new mongodb.ObjectID(uid1) },
          { users: new mongodb.ObjectID(uid2) }
        ]
      };
const items = Room.find(filter);

But it doesn't work. What's the mistake?

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'NativeTopology'
    |     property 's' -> object with constructor 'Object'
    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'
    --- property 'topology' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (F:\work\find-stuff\backend\node_modules\express\lib\response.js:1123:12)
    at ServerResponse.json (F:\work\find-stuff\backend\node_modules\express\lib\response.js:260:14)
    at F:\work\find-stuff\backend\src\controllers\room.controller.ts:75:27
    at Generator.next (<anonymous>)
    at F:\work\find-stuff\backend\src\controllers\room.controller.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (F:\work\find-stuff\backend\src\controllers\room.controller.ts:4:12)
    at createItem (F:\work\find-stuff\backend\src\controllers\room.controller.ts:59:16)
    at Layer.handle [as handle_request] 

I was very confusing about this.

3
  • 1
    Does it return an empty array or something else? Do you await/then the promise returned in the last line of code? Commented Feb 26, 2020 at 2:17
  • i think you make the search query wrong !. Commented Feb 26, 2020 at 4:11
  • Thanks, @mickl, I was very silly. Missing "await" was the problem. Please write the answer article. I'll accept it. Commented Feb 26, 2020 at 6:55

2 Answers 2

1

Model.find is an asynchronous operation which needs to be awaited / continued by using .then() or passing callback:

const items = await Room.find(filter);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code it should work

const filter = [
  { users: new mongodb.ObjectID(uid1) },
  { users: new mongodb.ObjectID(uid2) }
]

const items = Room.find({ $and: filter });

3 Comments

Sorry, but It occurs same exception.
In your find query you pass the object like filter ={.......} this not work becuase when pass the multiple condition in find query that should pass in array like filter=[]
I found the reason. Missing 'await' was the problem. Thanks for your care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.