How to get embedded object in mongodB shell

This is sample of my collection:

db.future_tasks.find().pretty()

{
        "_id" : "GhsHHRRPd9xgqpGTF",
        "number" : "CHG0267904",
        "userid" : "weeksxyz",
        "assigned_to" : "Paul W",
        "start_date" : "2016-08-24 10:00:00",
        "end_date" : "2016-08-24 17:00:00"
}

{
        "_id" : "gHJ9whgHDHGhchxs6",
         "number" : "TEST1d82c6",
        "userid" : "seb123",
        "assigned_to" : "Manual Testing",
        "number" : "TEST1d82c6",
        "count" : 1
}

then

typeof(db.future_tasks.find({userid:seb123}))

object

I would like to get specific attributes of certain user:

db.future_tasks.find({userid:seb123}).number  // Expected to get value of TEST1d82c6
db.future_tasks.find({userid:seb123}).count   // Expecte to get value of 1

why are they all not returning values as expected? What is the best practice in trying to get attributes? Thanks

because find returns a cursor. Try

db.future_tasks.findOne({userid:seb123}).number
1 Like

Hi Jamgold

What if I had two entries of userid: seb123, how to select the specific seb123’s attribute?

I’m not quite sure what you’re asking, but you can work with the returned cursor pretty much any way you want to. For example, if you want an array of task numbers (only) for all tasks that have a userid of seb123, you could do something like the following in your mongo console:

const taskNumbers = db.future_tasks.find({ 
  userid: 'seb123' 
}).map((task) => { 
  return task.name; 
});
taskNumbers;

The above will echo [ "TEST1d82c6", "SOMETHING", ... ].

You can also convert your cursor into an array at any point by using toArray:

const tasks = db.future_tasks.find({ userid: 'seb123' }).toArray();

I tried to change from

db.future_tasks.findOne({userid:'seb123'}).count

to

db.future_tasks.findOne({userid:'seb123'}).jobs

and collection attribute from count to jobs

But why is it thinking that it is now a method which cannot be found?

TypeError: Object [object Object] has no methods ‘jobs’

BUT it is not a method, just one of the collection key name I changed from count to jobs, why it is thinking it is a method?

Or is it just so happens that ‘count’ is a method with mongodB which coincides with my name ‘count’ in collection? So that it is calling the internal method ‘count’ instead of my ‘count’ attribute? So if I changed to ‘jobs’ then how to repeat that call db.future_tasks.findOne({userid:‘seb123’}).jobs because I thought this is calling the jobs object and not the method which now it cannot find?

The mongo shell behaves different from Meteor. Try connecting to your app you started on your system with meteor run by going into the directory of your project in a second window and type meteor shell. That will give you an interactive shell into the server side of your project.

meteor mongo or meteor shell?

so my question is

Is .count getting the count attribute of mongo collection which I defined or calling the count method? Maybe I used the wrong name count here!

meteor shell is what you want. In the mongodb shell (meteor mongo) you can not access properties of a document the way you are trying to do it.

db.future_tasks.findOne({userid:seb123}).number

this is getting the attribute .number in my collection so surely when I changed count to ‘jobs’ I should also find it but it is complaining that jobs is a method that cannot be found???

You are correct. mongodb shell does allow you to do that. Why don’t you post the output of

db.future_tasks.findOne({userid:seb123})

and also

db.future_tasks.find({userid:seb123}).count()

Hi Is it called synchronously or asynchronously, if asynchronously then I tried

db.future_tasks.findOne({userid:'seb123'}).count.then 

but it is not resolved? How to Promisify if so?

My question using switch case: