Home > database >  DB.collection('comments').find() not working
DB.collection('comments').find() not working

Time:11-05

findOne()

const solutionList =  await CommentMicroserviceDB.collection('comments').findOne({organizationId: ObjectId('5eb393ee95fab7468a79d189')});

Working Fine:-

 { 
    _id: 6364e934830dcc00b488848d,
    type: 'DOUBTS',
    typeId: 63622eb7ddf697001fdc7b0a,
    organizationId: 5eb393ee95fab7468a79d189,
    createdBy: 62d53063e34fe92f8a135818,
    status: 'Active',
    displayOrder: 2,
    createdAt: 2022-11-02T09:27:25.000Z,
    updatedAt: 2022-11-02T09:27:25.000Z,
    text:'<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n<p>Notification beofer berrnern</p>\n</body>\n</html>' 
}

find()

const solutionList =  await CommentMicroserviceDB.collection('comments').find({organizationId: ObjectId('5eb393ee95fab7468a79d189')});

But when I try with find() function is not working and give me:-

 Cursor {
 pool: null,
 server: null,
 disconnectHandler: Store { s: [Object], length: [Getter] },
 bson: BSON {},
 ns: 'comment_service.comments',
 cmd:
 { find: 'comment_service.comments',
 limit: 0,
 skip: 0,
 slaveOk: true },
 options:
 { readPreference: [ReadPreference],
   skip: 0,
   limit: 0,
   topology: [Server] },
 .....
 .....
 sortValue: undefined } 

Why this is happening ? findOne function is working perfectly but find not i need find here because i need all docs with given organizationId

Can anyone tell me Thanks!!!

CodePudding user response:

db.collection.find() returns a Cursor which is A pointer to the result set of a query. Clients can iterate through a cursor to retrieve results.

Refer to official collection.find() documentation.

If you just want the array of documents, try appending .toArray() at the end of your find query.

Try this:

const solutionList =  await CommentMicroserviceDB.collection('comments').find({
    organizationId: ObjectId('5eb393ee95fab7468a79d189')
}).toArray();

Read more about .toArray() here

  • Related