-2

I store documents from cars and want to get the temperature of all Mercedes cars as an array, how should the query be in Mongodb?

 { "_id" : { "$oid" : "5880ff305d15f416c89457b7" },
     "car" : "mercedes",
      "engine" : { 
            "sensor" : {
                        "temperatur" : "20",
                        "speed" : "100", 
                        "hue" : "40" 
                        }
                },
         "motor" : {
                    "Power" : "155", 
                    "Topspeed" : "400" 
                    } 
}

{ "_id" : { "$oid" : "5880ff305d15f416c89457b7" },
     "car" : "mercedes",
      "engine" : { 
            "sensor" : {
                        "temperatur" : "50",
                        "speed" : "100", 
                        "hue" : "40" 
                        }
                },
         "motor" : {
                    "Power" : "155", 
                    "Topspeed" : "400" 
                    } 
}

I would like to select the temperature for all Mercedes cars and receive it. result should be like [20,50]

EDIT: My code lookls like the following iam using JAVA:

  MongoClient mongoClient = new MongoClient();
      MongoDatabase database = mongoClient.getDatabase("test");
      MongoCollection<Document> coll = database.getCollection("myTestCollection");
s7vr
  • 73,656
  • 11
  • 106
  • 127
nosqldbms
  • 11
  • 4

1 Answers1

1

You can try something like this with regular query if you're okay with distinct values.

db.cars.distinct("engine.sensor.temperatur", {"car" : "mercedes"});

This will give you

[ "20", "50" ]

Update - Java equivalent:

List<String> temps = coll.distinct("engine.sensor.temperatur", new Document("car", "mercedes"), String.class).into(new ArrayList<>());

Update - Aggregation Option

Bson match = new Document("$match", new Document("car", "mercedes"));

Bson group = new Document("$group", new Document("_id", "$car").append("temps", new Document("$push", "$engine.sensor.temperatur")));

List<String> temps  = (List<String>) coll.aggregate(Arrays.asList(match, group)).map(document -> document.get("temps")).first();
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • does cars mean the collection name? and how should I do this with my test database and myTestCollection collection? – nosqldbms Jan 19 '17 at 18:58
  • yes it is. Just replace `cars` with `myTestCollection` – s7vr Jan 19 '17 at 18:59
  • and db? should I replace it with database? I get an error. syntax error on tokes(s), misplaced contruct – nosqldbms Jan 19 '17 at 19:01
  • First change to `test` db. Something like `use test` and then run the query. Added the java equivalent too to the answer. – s7vr Jan 19 '17 at 19:02
  • That was for mongo console query. – s7vr Jan 19 '17 at 19:04
  • could you please provide me an example with all values and not distinct? I tried aggregate, but its not working – nosqldbms Jan 19 '17 at 19:15
  • Updated with aggregation option. – s7vr Jan 19 '17 at 19:51
  • error on "document ->" "Lambda expression's parameter document cannot redeclare another local variable defined in an enclosing scope. " – nosqldbms Jan 19 '17 at 21:12
  • You've code where `document` is already defined. You can rename this lambda variable if it is conflicting. See if you can relate your issue to this http://stackoverflow.com/questions/22773003/variable-is-already-defined-in-method-lambda – s7vr Jan 19 '17 at 21:20