简体   繁体   中英

mongodb get all subdocuments from all documents

I have this sample documents

 { "_id" : ObjectId("549452820d56c14be9473f30"), "company" : "google", "customers" : [{ "cart" : "34323", "logins" : [{ "Id" : 1, "Date" : ISODate("2013-02-21T12:54:52Z"), }, { "Id" : 2, "Date" : ISODate("2013-02-21T12:55:10Z"), }] }] } { "_id" : ObjectId("549452820d56c14be9473f31"), "company" : "microsoft", "customers" : [{ "cart" : "14423", "logins" : [{ "Id" : 6, "Date" : ISODate("2013-02-21T12:58:09Z"), }, { "Id" : 7, "Date" : ISODate("2013-02-21T12:57:50Z"), }, { "Id" : 8, "Date" : ISODate("2013-02-21T12:58:18Z"), }] }] } 

  1. I would like to know how I can, for instance, get all the logins from all costumers in both Linq and js language (if it's possible). This should be pretty simple but I'm just not getting it. The trick here is that I would like the query only to return me the logins data without the rest of the document fields...

  2. I also would like to know how I could get a whole document based on the "cart" property.

The below Mongo queries may help you.

db.test.find({},{"customers.logins":true,_id:false})

will return login details alone.

db.test.find({"customers.cart":"34323"})

will get a whole document based on the "cart" property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM