Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
I am not able to use result.ops method to view All the documents inserted using insertMany

i am trying to insert new documents in my task collection and the print all the documents inserted by using result.ops method

const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient

const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'task-manager'

MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
    return console.log('Unable to connect to database!')
}

const db = client.db(databaseName)

db.collection('tasks').insertMany([
    {
        description: 'Clean the house',
        completed: true
    },{
        description: 'Renew inspection',
        completed: false
    },{
        description: 'Pot plants',
        completed: false
    }
], (error, result) => {
    if (error) {
        return console.log('Unable to insert tasks!')
    }
    console.log(result.ops)  //returns undefined
    console.log(result)   // returns only 2 methods 
})
})   

// console.log returns only 2 methods insertedcounts & insertedids

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

https://stackoverflow.com/a/68424173/16483030 Here is your answer. In version 4 insertOne returns insertOneResult that have only 2 properties: acknowledged and insertedId. So you can use insertedId. That makes sense because if it is one, an array is not necessary.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!