2

so I went into the mongo shell ./mongo and did the following commands:

>>>use mydb
>>>show collections
myc
myotherc
users

>>>db.myc.find()
{ "_id" : ObjectId("5c8dd1c7b350e73a6bc7cf50"), "name" : "mfirst", "theowner" : ObjectId("5c8d7146bc279c28a6ded7b2"), "mowner" : ObjectId("5c8dcb3a7f1b20386577d4bc"), "created" : ISODate("2019-03-17T04:49:11.194Z"), "__v" : 0 }
{ "_id" : ObjectId("5c8dd4dfb547843bdee5b9bd"), "name" : "mlast", "theowner" : ObjectId("5c8d7146bc279c28a6ded7b2"), "mowner" : ObjectId("5c8dcb3a7f1b20386577d4bc"), "created" : ISODate("2019-03-17T05:02:23.723Z"), "__v" : 0 }

>>>db.myotherc.find()
{ "_id" : ObjectId("5c8dcb3a7f1b20386577d4bc"), "people" : [ ObjectId("5c8d7146bc279c28a6ded7b2") ], "name" : "thename", "owner" : ObjectId("5c8d7146bc279c28a6ded7b2"), "created" : ISODate("2019-03-17T04:21:14.388Z"), "__v" : 0 }

>>>db.myc.find({"mowner._id":"5c8dcb3a7f1b20386577d4bc"})

>>>db.myc.find({"mowner.name":"thename"})

Both the last two queries return nothing, even though I think I have the syntax correct and it does exist. Any idea why?

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • If you are using mongoose, as the tags say, you want to populate the fields with the object ids https://stackoverflow.com/questions/38051977/what-does-populate-in-mongoose-mean – Haris Bouchlis Mar 17 '19 at 17:35
  • 1
    Your query should be `db.myc.find({"mowner": Objectid("5c8dcb3a7f1b20386577d4bc")})` – Ashh Mar 17 '19 at 17:39
  • @AnthonyWinzlet doesn't work on my end. (returns nothing still) – SilentDev Mar 17 '19 at 17:45
  • I misspelled the ObjectId. Try it again. It should work – Ashh Mar 17 '19 at 17:52
  • 1
    @HarisBouchlis I feel like it should still return something. `populate` just populates a field. My query should still return the object `myc` (without `mowner` populated). Right? I tried though with populate but still returns nothing. – SilentDev Mar 17 '19 at 17:54
  • @AnthonyWinzlet I tried it with the fix. Still doesn't work. I doing it from within the shell `./mongo` but it doesn't work with mongoose too. – SilentDev Mar 17 '19 at 17:55

2 Answers2

0

Try :

db.myc.find({"name":"thename"})
db.myc.find({"mowner": ObjectId("5c8dcb3a7f1b20386577d4bc")})
Moad Ennagi
  • 1,058
  • 1
  • 11
  • 19
0

db.myc.find()

{ "_id" : ObjectId("5c8dd1c7b350e73a6bc7cf50"), "name" : "mfirst", "theowner" : ObjectId("5c8d7146bc279c28a6ded7b2"), "mowner" : ObjectId("5c8dcb3a7f1b20386577d4bc"), "created" : ISODate("2019-03-17T04:49:11.194Z"), "__v" : 0 }

{ "_id" : ObjectId("5c8dd4dfb547843bdee5b9bd"), "name" : "mlast", "theowner" : ObjectId("5c8d7146bc279c28a6ded7b2"), "mowner" : ObjectId("5c8dcb3a7f1b20386577d4bc"), "created" : ISODate("2019-03-17T05:02:23.723Z"), "__v" : 0 }

In myc collection,mowner and name both are the flat fields to the collection. Therfore you need to change the query to :-

db.myc.find({"name":"thename"})
db.myc.find({"mowner": ObjectId("5c8dcb3a7f1b20386577d4bc")})
Community
  • 1
  • 1
Nishant Bhardwaz
  • 924
  • 8
  • 21