mongodb-query

How create a new array field with the aggregate framework

青春壹個敷衍的年華 提交于 2019-12-04 04:37:48
I'm starting to use mongoDb and I'm stuck with a simple use case. Let's say I've got a collection 'aCollection' with entries such as this: { _id: ObjectId(123), lat: 48,56623, long: 2,56332 } and I want to create a new collection with entries like this: { _id: ObjectId(123), lat: 48,56623, long: 2,56332, geometry : { type: "Point", coordinates: [48,56623, 2,56332] } } I thought about the aggregation framework: db.aCollection.aggregate([{$project: { _id: 1, lat: 1, long: 1, geometry: { type: {$concat: ["Point"]}, coordinates: ["$lat", "$long"] } }}]) But it ain't working and I get this

Remove Object from Nested Array by Multiple Criteria

走远了吗. 提交于 2019-12-04 04:34:03
问题 I have this schema document in mongoDB: { "_id": UUID("cf397865-c000-4f51-8959-1aae84769706"), "CreationDateTime": ISODate("2016-05-06T05:09:14.589Z"), "WKT": "", "Distributions": [{ "_id": UUID("bb95bedb-4baa-4ada-90b1-0d763e70ebfe"), "DeliveryType": 1, "DistributionData": [{ "Key": "Topic", "Value": "Topics", "Children": null }, { "Key": null, "Value": null, "Children": null }, { "Key": "Message", "Value": "test", "Children": null } ], "Schedules": [ ISODate("2016-05-06T05:09:56.988Z") ] }

Most efficient way to change a string field value to its substring

时光毁灭记忆、已成空白 提交于 2019-12-04 03:50:12
问题 I have a collection filled with documents that look like this: { data: 11, version: "0.0.32" } and some have a test suffix to version : { data: 55, version: "0.0.42-test" } The version field has different values but it always conforms to the pattern: 0.0.XXX . I would like to update all the documents to look like this: { data: 11, version: 32 } and the suffixed version (for test documents - version should be negative): { data: 55, version: -42 } The collection with these documents is used by

mongotemplate aggregation with condition

房东的猫 提交于 2019-12-04 03:47:54
问题 I have a collection where the documents look like this: { _id: "545b9fa0dd5318a4285f7ce7", owner: "admin", messages: [ { id: "100", status: "sent", note: "" }, { id: "100", status: "pending", note: "" }, { id: "101", status: "sent", note: "" }, { id: "102", status: "sent", note: "" }, { id: "101", status: "done", note: "" } ] } (This is just a short example, in my case the sub array is very large) I need to query the collection and get some statistics for a specific document. So in this

Encrypt password fields in mongodb

馋奶兔 提交于 2019-12-04 03:38:52
I have following code, it insert the userName and password into database but the password is stored in plain text format. I mean when I'll look into the db I can see the inserted password. I want to store password in encrypted format MongoClient client = new MongoClient("localhost",27017); DB db = client.getDB("Test"); DBCollection collection = db.getCollection("EncryptionDemo"); BasicDBObject documentDetail = new BasicDBObject(); documentDetail.put("userName", "admin12"); documentDetail.put("password", "12345"); collection.insert(documentDetail); How can I achieve this? According to the

Project with Match in aggregate not working in mongodb

£可爱£侵袭症+ 提交于 2019-12-04 03:31:47
问题 I am trying to fetch data based on some match condition. First I've tried this: Here ending_date is full date format Offer.aggregate([ { $match: { carer_id : req.params.carer_id, status : 3 } }, { $group : { _id : { year: { $year : "$ending_date" }, month: { $month : "$ending_date" }}, count : { $sum : 1 } } }], function (err, res) { if (err) ; // TODO handle error console.log(res); }); which gives me following output: [ { _id: { year: 2015, month: 11 }, count: 2 } ] Now I want to check year

MongoDB aggregate fields without knowing all the fields before hand

血红的双手。 提交于 2019-12-04 03:31:37
问题 How can I compute the aggregate of the following metrics without knowing all the metrics before hand? Can I accomplish this using the aggregate framework or MapReduce? [ { player_id: '123', timestamp: <sometime>, metrics: { points_per_game: 1, rebounds_per_game: 2, assist_per_game: 3, } }, { player_id: '123', timestamp: <sometime>, metrics: { points_per_game: 1, rebounds_per_game: 2, } }, { player_id: '345', timestamp: <sometime>, metrics: { points_per_game: 1, rebounds_per_game: 2, point_in

$geoNear (aggregate pipeline) not returning correct documents

坚强是说给别人听的谎言 提交于 2019-12-04 03:28:23
问题 I am not getting the correct results returned when using $geoNear in the aggregate pipeline. The same query using a typical find() query (using $near ) does in fact return the right results. BUT , when removing the equality condition (on schedule.key ), both queries return the correct data. $geoNear using aggregate pipeline: db.place.aggregate( [ { $geoNear: { spherical: true, near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] }, distanceField: "dist" } }, { $match: { "schedule.key

How to get the maximum value of a field for each group with the array of corresponding documents?

梦想与她 提交于 2019-12-04 03:21:30
I have a collection like { "_id" : ObjectId("5738cb363bb56eb8f76c2ba8"), "records" : [ { "Name" : "Joe", "Salary" : 70000, "Department" : "IT" } ] }, { "_id" : ObjectId("5738cb363bb56eb8f76c2ba9"), "records" : [ { "Name" : "Henry", "Salary" : 80000, "Department" : "Sales" }, { "Name" : "Jake", "Salary" : 40000, "Department" : "Sales" } ] }, { "_id" : ObjectId("5738cb363bb56eb8f76c2baa"), "records" : [ { "Name" : "Sam", "Salary" : 90000, "Department" : "IT" }, { "Name" : "Tom", "Salary" : 50000, "Department" : "Sales" } ] } I want to have the results with the highest salary by each department {

group in mongo excluding null values

China☆狼群 提交于 2019-12-04 02:46:57
问题 I have mongo query which does the group operation on the documents. I have almost got the expected results except that I want to refine the results without empty or null values. Currently my query looks like this: db.productMetadata.aggregate([{$group:{"_id":{"color":"$productAttribute.colour","gender":"$productAttribute.gender"},"count" : {$sum : 1}}}]); And the results looks something like this: { "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 } { "_id" : { }, "count" : 4 } { "