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

Viewed 1359

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

1 Answers

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.

Related