The Result Object OPS not Showing in GraphQL with nodejs

Hey, I’m building a GraphQL API with NodeJS and MongoDB. Everything was going smooth until i tried saving data to the database and it didn’t return any OPS RESULT in the terminal. Please have a look at the code for better understanding and guide me on where did I go wrong? Thank you in advance!

Welcome to the MongoDB Community Forums @run_x_todo !

Can you provide more information to help understand your issue:

  • Version of MongoDB Node.js driver
  • Version of MongoDB server
  • Expected outcome

In the MongoDB Node.js 4.0 driver, insert() returns an InsertManyResult which has the 3 properties in your screenshot: acknowledged, insertedCount, and insertedIds.

Since the insert() method is deprecated (per the warning in your screenshot), I’d recommend using insertOne(), insertMany(), or bulkWrite() to ensure you get the expected result object returned.

Regards,
Stennie

Hey, thank you for your response.

  1. MongoDB Node.js 4.1 driver
  2. MongoDB Atlast Cloud
    I’m building graphQL API with NodeJs and MongoDB and for some weird reasons, MongoDB isn’t showing result ops in the terminal like the previous driver version below 4.0 (previous version example attached)

I want to return “RESULT” to see inserted data but I’m not being able to do it, can you please help me with how I can do that? how do in return it? I’m losing all of my brain cells lol

before I’d just add;
console.log(result); terminal would show result with an array OPS then id add;
return result.ops[0], before highlighted code and it’d return the inserted data result

//see attached screenshot for better understanding// Thanks in advance!

mongo

how should i return this below attached code if i’m not getting array OPS after console.log(result);?

@Stennie
fb

Ever figure this out? I’m having the exact same issue, not sure how to get the result object show the ops with graphql and nodejs.

any help would be greatly appreciated :pray:

Hey Man it’s because of Mongo 4.x update where we no longer have ops instead of that we can do something like this after insert.

const result = await db.collection('Users').insertOne(newUser);
      const someId = result.insertedId;

InsertId and acknowledgement are the only response we get from the db.

you have to run another db query here

const actualResult = await db
        .collection('Users')
        .findOne({ _id: someId });

and if you do console log you will get the inserted document :slight_smile:

Happy Coding