0

Let me start by saying I am completely new when it comes to mongodb. I'm working on a project which stores data for customers in a customers collection, phones in a phones collection and orders they have made in an orders collection. The orders will just be the id of the customer and the id of the phones ordered. I have created the phones collection by inserting 10 random entries from an array of 40. What I'm trying to do is call on the phones collection and take a random id from there and add it to the order. In straight mongodb I got it to work using db.phones.find({}, {manufacturer: 0, model: 0, price: 0}).limit(-1).skip(db.phones.count()*_rand()).next() which returns a random id and only an id for one of the phones in the phone collection. This same approach doesn't work with nodejs.

I have tried running the command in node using db.collection('phones').find({}, {manufacturer: 0, model: 0, price: 0}); to get all the ids in the phones collection and then try manipulate these but it returns an object Object which I can't access. I've tried just console.log on the first element saving it to a phoneCollection var then trying phoneCollection[0]; and it comes out undefined.

I am aware I am very likely making many rookie mistakes here so sorry if I'm not seeing the obvious.

For reference, the function I'm using is below, the connection is running fine and opened in a function call higher up. This method works for my other inserts no problem, it's this insert I'm having issues with.

const insertOrders = function(db, callback){
    const collections = db.collection('orders');

    console.log("phone test "+db.collection('phones').find({}, {manufacturer: 0, model: 0, price: 0})); 
//this just prints out [object Object]

    var phoneCollection = db.collection('phones').find({}, {manufacturer: 0, model: 0, price: 0});

    console.log("Phone collection " +phoneCollection);
    //same as previous, just wanted to test if assigning
 it would be different and also tried 
phoneCollection[0] which comes back undefined.

    const phone1 = db.collection('phones').find({}, 
{manufacturer: 0, model: 0, price: 0})
.limit(-1).skip(db.collection('phones').estimatedDocumentCount()*Math.floor(Math.random()))
.next();
    console.log("phone one "+phone1);
//this comes back as an [object Promise]

    collections.insertMany([
    {"customerID": 1,
    "phoneOrdered": phone1}, {"customerID": 3, "phoneOrdered" : phone1, "phoneOrdered": phone1},
    ], function(err, result) 
    {
        console.log(result);
        callback(result);
    });

}

Any insights anyone can help with would be greatly appreciated.

Peter
  • 1

1 Answers1

0

To only get the id use select:

var phoneCollection = db.collection('phones').find({}, {manufacturer: 0, model: 0, price: 0});

var myDocument = phoneCollection.hasNext() ? phoneCollection.next() : null;

if (myDocument) {
    var myID = myDocument._id;
    print (tojson(myID));
}

While doing console log use , rather than +:

console.log("Phone collection " ,phoneCollection);
Nayan
  • 638
  • 6
  • 15
  • Thank you for the reply. I just tried this but it gave me a TypeError saying the .select is not a function – Peter Apr 30 '20 at 18:34
  • Check [this](https://stackoverflow.com/questions/24348437/mongoose-select-a-specific-field-with-find) if you are using something like mongoose. If note you can remove select and go with `phoneCollection._id` – Nayan Apr 30 '20 at 18:38
  • I'm not using mongoose at all and tried the phoneCollection._id but it just came back undefined. I'm assuming you meant in the console.log. – Peter Apr 30 '20 at 18:48
  • What is coming in console log with , for `phoneCollection` only? – Nayan Apr 30 '20 at 18:50
  • I got it now I guess the issue might be with the return so according to official documentation of MongoDB, find() returns a cursor. so I guess we need to use something like `phoneCollection[0]._id`. Please check – Nayan Apr 30 '20 at 18:58
  • Just checked that and it comes back as a TypeError: Cannot read property '_id' of undefined. I did get a cursor object in the previous console.log with just phoneCollection – Peter Apr 30 '20 at 19:07
  • My bad with reading object from cursor. Got the answer from official mongodb doc and updated the answer. please try it and also this is [reference](https://docs.mongodb.com/manual/reference/method/db.collection.find/) – Nayan Apr 30 '20 at 19:11