0

query mongo to find the count of all cars array in each document of collection company

I am new to mongo, I

db.company.find() --> but then how do I select the arrays and that too for all of them

collection company : {

    {   
       "_id": "5b8ed214b460e7c17c5a33f9",
        "company_location": "USA",
        "company_name": "gmc",
        "__v": 0,
        "cars": [{
                "_id": "5b8ed214044b2509466eca2e",
                "model": "TERRAIN",
                "year": 2013,
                "PriceInINR": 3851710,
                "trim": "SLE2 FWD",
                "engine": "SPORT UTILITY 4-DR",
                "body": "2.4L L4 DOHC 16V FFV",
                "color": "Yellow",
                "transmission_type": "Manual",
                "dealer_id": "5b8e7ce7065fa50bee095072"
            },
              {------},
              {------}   
}
user6202188
  • 540
  • 1
  • 6
  • 13
  • 1
    Try `db.company.aggregate({"$group":{"_id":null,"count":{"$sum":{"$size":"$cars"}}}})` – s7vr Sep 04 '18 at 21:36
  • @Veeram hey can you help me with one more thing – user6202188 Sep 04 '18 at 21:41
  • Sure. What is it ? – s7vr Sep 04 '18 at 21:46
  • @Veeram from last 5-6 hours I am trying to push data into mongo. and this is 12726 son objects. and it doesn't not work, like I don't know it works or not, so now finally when there is no way, I have break up 1400 json object into 7 .json files with each containing 200 objects. and now it works atleast. so I am proceeding to build other things. --> which is the main functionality, all the time is just wasted in pushing their data into mongo which I have miserably failed. can you help me with it. **its really important project** – user6202188 Sep 04 '18 at 21:50
  • I would recommend creating a separate question with all the details as what is your use case, expected input and output and I'm sure someone will be able to help you. – s7vr Sep 04 '18 at 21:54
  • @Veeram I tried it, writing but I wasn't able to frame my problem exactly its too big to write and explain I have already tried, – user6202188 Sep 04 '18 at 21:57
  • @Veeram hey last question man : in the above document can I directly find the car (object inside array) as it also has `_id` that means it is also indexed by mongo so I should be able to access it by the `findById()` method, if it is possible how can I find one specific car given I have its id – user6202188 Sep 04 '18 at 22:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179418/discussion-between-user6202188-and-veeram). – user6202188 Sep 04 '18 at 22:07
  • This will [help](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) you – s7vr Sep 04 '18 at 22:15

1 Answers1

0

First, assign the query to the variable... then count cars array... like below

var obj = db.company.find(_id:"5b8ed214b460e7c17c5a33f9").toArray();

var count = obj[0].cars.length

If you will find by _id obj[index] always will be 0.

Martin
  • 1
  • 1
  • get("/car/{id}", (req, res) => { // ? detail of specific car *how can I* query the above collection to find the given car in the `cars` array present in each document, I think we should be able to find it fast as it has `_id` with it so it means mongo would have index it – user6202188 Sep 05 '18 at 06:37
  • app.get("/car/:id", (req, res)=>{ var car = req.params.id db.collection.find(_id: car)}) – Martin Sep 05 '18 at 07:02
  • `let car_id = req.params.id; CompanyModel.find({ _id: car_id }, (err, car) => { if (err) { res.statusMessage = "Error : while getting specific car"; res.status(400).end(); return; } res.status(200); res.send(car); });` **what you have told gives the companies and not the cars plz see this : https://pastebin.com/ZxW525yB** – user6202188 Sep 05 '18 at 07:37
  • @user6202188 I think you can use the same answer of your previous question https://stackoverflow.com/questions/52178881/mongo-query-getting-a-specific-object-its-id-is-known-from-array-of-objec/52179116#52179116 – Anouar Kacem Sep 05 '18 at 08:42
  • @AnouarKacem the problem is its is not sure I don't know the company _id, **https://pastebin.com/ZxW525yB** all the separate objects specify one company, and each of them have cars --> list of car – user6202188 Sep 05 '18 at 09:27
  • db.data.find({"cars._id": '5b8ef8b7d1a7c2156417de72'},{"cars.$.model":1}).toArray() - returns One car - tested on mongo shell and MongoDB Compass – Martin Sep 05 '18 at 20:51