mongodb-query

MongoDB Aggregation Query- Rename Fields Returned from Within Embedded Documents

喜欢而已 提交于 2019-12-19 09:13:26
问题 I'm currently using aggregate operator to return documents that has an array of embedded (sub) documents. I want to rename the field name for the array and also rename fields names in the embedded documents of the array. As an example, for projection I want to rename the array from "friends" to "buddies" and I also want to rename fields in the embedded document from "name" to "nickName". Can I do this within an aggregate operation and if so how? Here's an example of the source document: [ {

MongoDB Aggregation Query- Rename Fields Returned from Within Embedded Documents

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 09:13:23
问题 I'm currently using aggregate operator to return documents that has an array of embedded (sub) documents. I want to rename the field name for the array and also rename fields names in the embedded documents of the array. As an example, for projection I want to rename the array from "friends" to "buddies" and I also want to rename fields in the embedded document from "name" to "nickName". Can I do this within an aggregate operation and if so how? Here's an example of the source document: [ {

Java MongoDB 3.0 driver query distinct without filter

匆匆过客 提交于 2019-12-19 08:54:06
问题 How may I query distinct with the Java MongoDB 3.0 driver? I am attempting to query unique categories records from a locations collection in MongoDB. In the Mongo shell, this is very simple: db.locations.distinct("categories"); In Java, it's not the same. MongoClient client = new MongoClient(); MongoDatabase db = client.getDatabase("yelp"); //this will not compile, although examples from before 3.0 do it this way MongoCursor<Document> c = db.getCollection("locations").distinct("categories")

pymongo group by datetime

余生长醉 提交于 2019-12-19 08:53:34
问题 Im trying to search through a collection and group records by date field which is a datetime. I know pymongo converts those to the proper type on the background (ISODate or something like that). Question is, since datetime objects have date, time, timezone.. how can i tell the group operator to use only the date portion? Because otherwise i dont get the desired grouping since time is preventing the records with same day, month, year to be grouped together. db.test.aggregate([ {"$group": { "

Operator $arrayElemAt in aggregation with Mongo < 3.2

浪子不回头ぞ 提交于 2019-12-19 07:39:31
问题 Using the aggregation framework in Mongo, How can I achieve the same result in Mongo < 3.2 as in Mongo 3.2 with the operation $arrayElemAt ? Example in Mongo 3.2 Collection { "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] } Query db.users.aggregate([ { $project: { name: 1, first: { $arrayElemAt: [ "$favorites", 0 ] }, last: { $arrayElemAt: [ "$favorites", -1 ] } } } ]) It works fine But, I have been forced to used Mongo 3.0 so I can't use this operator,

MongoDB Insert key with '$' (dollar)

我与影子孤独终老i 提交于 2019-12-19 03:15:29
问题 I am trying to insert a collection with a json object containing a key which starts with ' $ ' (for instance: ' $count '). I read the mongodb v3.0 FAQ's and they have mentioned it is not such a key. Is there any roundabout way insert such a key and retrieve it back? 回答1: Field names cannot contain dots (i.e. .) or null characters, and they must not start with a dollar sign (i.e. $). In some cases, you may wish to build a BSON object with a user-provided key. In these situations, keys will

query in mongo Shell gives SyntaxError: missing : after property

╄→гoц情女王★ 提交于 2019-12-19 03:12:21
问题 db.movieDetails.find( { year: 2013, imdb.rating: Pg-13, award.wins: 0 }, { title: 1, _id: 0 } ).pretty(); The mongo shell returns this error 2016-08-13T09:08:00.648+0200 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:60 Why? Thank you in advance! 回答1: If your query includes inner documents, then use quotes for them. Also, use quotes for querying String values db.movieDetails.find( { year: 2013, "imdb.rating": "Pg-13", "award.wins": 0 }, { title: 1, _id: 0 } ).pretty();

$project in $lookup mongodb

假装没事ソ 提交于 2019-12-19 03:09:02
问题 I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects ( user_detail ) that contains more data that i need. I want only two fields ( scheduleStart and scheduleEnd ) of my result. My query: User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf", phone: "$phone", email: "$email", birthday: "$birthday", lastName: "$lastname" },

$project in $lookup mongodb

耗尽温柔 提交于 2019-12-19 03:08:36
问题 I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects ( user_detail ) that contains more data that i need. I want only two fields ( scheduleStart and scheduleEnd ) of my result. My query: User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf", phone: "$phone", email: "$email", birthday: "$birthday", lastName: "$lastname" },

MongoDB : how to set a new field equal to the value of another field, for every document in a collection [duplicate]

本小妞迷上赌 提交于 2019-12-18 17:36:29
问题 This question already has answers here : Update MongoDB field using value of another field (8 answers) Closed 3 years ago . I need to run a migration script to insert a value (already available in each document) into an array of this same document. This has to be done for each documents of my collection (no selection query required) How to change this: { "_id": ObjectID("5649a7f1184ebc59094bd8b3"), "alternativeOrganizer": ObjectID("5649a7f1184ebc59094bd8b1"), "myArray": [] } Into this: { "_id