Mongo即使存在也不会返回文件

时间:2019-03-17 17:27:14

标签: mongodb mongoose mongodb-query

所以我进入了mongo shell ./mongo,并执行了以下命令:

>>>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"})

即使我认为我的语法正确并且确实存在,后两个查询均不返回任何内容。知道为什么吗?

2 个答案:

答案 0 :(得分:2)

  

db.myc.find()

     

{“ _id”:ObjectId(“ 5c8dd1c7b350e73a6bc7cf50”),“名称”:“ mfirst”,   “ theowner”:ObjectId(“ 5c8d7146bc279c28a6ded7b2”),“ mowner”:   ObjectId(“ 5c8dcb3a7f1b20386577d4bc”),“创建”:   ISODate(“ 2019-03-17T04:49:11.194Z”),“ __v”:0}

     

{“ _id”:ObjectId(“ 5c8dd4dfb547843bdee5b9bd”),“ name”:“ mlast”,“ theowner”:   ObjectId(“ 5c8d7146bc279c28a6ded7b2”),“所有者”:   ObjectId(“ 5c8dcb3a7f1b20386577d4bc”),“创建”:   ISODate(“ 2019-03-17T05:02:23.723Z”),“ __v”:0}

myc collection中,mownername都是集合的平面字段。因此,您需要将查询更改为:-

db.myc.find({"name":"thename"})
db.myc.find({"mowner": ObjectId("5c8dcb3a7f1b20386577d4bc")})

答案 1 :(得分:0)

尝试:

db.myc.find({"name":"thename"})
db.myc.find({"mowner": ObjectId("5c8dcb3a7f1b20386577d4bc")})