I am getting [object object]

Hi, I am Govind Bisen,
I am a fan of MongoDB and I want to learn nodejs to MongoDB atlas connection to connect my front-end react application.
I am facing some issues below is the code that returns values as [object Object] but as a result, i want to have the whole collection. Please help me.

code :

const {MongoClient} = require('mongodb');

async function main(){

    const uri = "mongodb+srv://user:password@cluster0.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";

    const client = new MongoClient(uri);

    try {

        await client.connect();

        // We can call whatever function we want to call.

        //await listDatabases(client);  

        // await createListing(client,{

        //     name:"lovely",

        //     summary:"A lovely room",

        //     bedrooms:1

        // })

        // await createMultipleListings(client,[

        //     {name:"lovely",

        //     summary:"A lovely room",

        //     bedrooms:1},

            

        //     {name:"Badly",

        //     summary:"A BAdly room",

        //     bedrooms:1}

        //     ]

        //     )

        findOneListingByName(client);

    } catch (error) {

        console.log(error);

    }

    finally{

        await client.close();

    }

}

main().catch(console.error);

//This will insert multiple document 

async function createMultipleListings(client,newListings){

    const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertMany(newListings);

    console.log(`${result.insertedCount.toString()} new listing with id: ${result.insertedIds}`);

}

// this  will only insert one document

async function createListing(client,newListing){

    const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing);

    console.log(`new listing with id: ${result.insertedId.toString()}`);

}

//show all data or one data findOne, find

async function findOneListingByName(client){

    const result = await client.db("sample_airbnb").collection("listingsAndReviews").find({});

    if(result){

        console.log(`collection  : ${result}`);  //JSON.stringify(result)// .lean

    }

}

// Show all databases 

async function listDatabases(client){

     const databaselist = await client.db().admin().listDatabases();

     console.log("databases - ");

     databaselist.databases.forEach(db => {

     console.log(` - ${db.name}`);

     });

}

output:

C:\Users\govin\OneDrive\Desktop\node-to-mongoCrud> node demo.js
collection  : [object Object]

Hi @Govind_Bisen and welcome in the MongoDB Community :muscle: !

Here is my attempt. Hopefully the code is self explanatory and will help you track and fix your mistakes.

const {MongoClient} = require('mongodb');
const uri = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const client = new MongoClient(uri);

async function main(){
  try {
    await client.connect();
    await findAirbnbWithLimit(client, 2);
    await findAirbnbWithLimitWithAnArray(client, 5);
  } catch (error) {
    console.log(error);
  } finally{
    await client.close();
  }
}

main().catch(console.error);

async function findAirbnbWithLimit(client, limit) {
  const cursor = client.db("sample_airbnb").collection("listingsAndReviews").find().limit(limit);
  await cursor.forEach(doc => console.log(doc));
}

async function findAirbnbWithLimitWithAnArray(client, limit) {
  const arrayDocs = await client.db("sample_airbnb").collection("listingsAndReviews").find().limit(limit).toArray();
  console.log(`\n==> There are ${arrayDocs.length} docs in this array.`);
}

If you want to learn quickly MongoDB + Node.js, I highly recommend following the M220JS free course on MongoDB University and potentially the M001 before that.

Cheers,
Maxime.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.