mongodb-query

MongoDB Aggregate lookup in Go (mgo.v2)

家住魔仙堡 提交于 2019-12-10 15:56:09
问题 I'm trying to implement $lookup functionality in one of my mongoDB queries in go (golang) using the mgo package. Below are my collections: folders: "_id" : ObjectId("22222222222222"), "name" : "Media", "level" : 1, "userIDs": [ObjectId("4444444444444")] documents: "_id" : ObjectId("11111111111111"), "title" : "Media Management", "body" : BinData(0,"PvQ6z2NBm4265duo/e2XsYxA5bXKo="), "level" : 1, "folderID" : ObjectId("22222222222222"), // Foreign Key/Field "userIDs" : [ObjectId("44444444444444

Manually supplying arguments to a MongoDB query to support collation feature (for case insensitive index)

微笑、不失礼 提交于 2019-12-10 15:56:02
问题 I have installed the current development version 3.3.11 in order to test the case insensitive index that is apparently supported according to https://jira.mongodb.org/browse/SERVER-90. I have tried this from a mongo shell and a simple test database and it does seem to work. Unfortunately, even though one specifies collation (and strength) during index creation, one must also specify the same collation params with .find in order to get case insensitive matches. If collation is omitted from the

mongodb aggregation framework group by two fields

被刻印的时光 ゝ 提交于 2019-12-10 15:05:35
问题 I'm querying my database using aggregation and pipeline, with two separate queries: $groups_q = array( '$group' => array( '_id' => '$group_name', 'total_sum' => array('$sum' => 1) ) ); $statuses_q = array( '$group' => array( '_id' => '$user_status', 'total_sum' => array('$sum' => 1) ) ); $data['statuses'] = $this->mongo_db->aggregate('users',$statuses_q); $data['groups'] = $this->mongo_db->aggregate('users',$groups_q); And I'm getting what I want: Array ( [statuses] => Array ( [result] =>

Converting some fields in Mongo from String to Array

大城市里の小女人 提交于 2019-12-10 14:31:11
问题 I have a collection of documents where a "tags" field was switched over from being a space separated list of tags to an array of individual tags. I want to update the previous space-separated fields to all be arrays like the new incoming data. I'm also having problems with the $type selector because it is applying the type operation to individual array elements, which are strings. So filtering by type just returns everything. How can I get every document that looks like the first example into

MongoDB Update array element (document with a key) if exists, else push

蓝咒 提交于 2019-12-10 14:18:18
问题 I have such a schema: doc: { //Some fields visits: [ { userID: Int32 time: Int64 } ] } I want to first check if a specific userID exists, if not, push a document with that userID and system time , else just update time value. I know neither $push nor $addToSet are not able to do that. Also using $ with upsert:true doesn't work, because of official documentation advice which says DB will use $ as field name instead of operator when trying to upsert. Please guide me about this. Thanks 回答1: You

MongoDB Java Driver: MongoCore Driver vs. MongoDB Driver vs. MongoDB Async Driver

僤鯓⒐⒋嵵緔 提交于 2019-12-10 13:55:48
问题 There are three different driver options for the MongoDB Java driver: Core Driver MongoDB Driver MongoDB Async Driver The drivers description page gives a brief description of each of them but no further explanation is provided regarding when I should use each of them. My question: can you, please, clarify what are the cases to use each of them? When should I prefer one over the second and when I must/have to use the particular driver option? 回答1: TL;DR : Use the async driver if the

Can I use populate before aggregate in mongoose?

谁说我不能喝 提交于 2019-12-10 13:33:56
问题 I have two models, one is user userSchema = new Schema({ userID: String, age: Number }); and the other is the score recorded several times everyday for all users ScoreSchema = new Schema({ userID: {type: String, ref: 'User'}, score: Number, created_date = Date, .... }) I would like to do some query/calculation on the score for some users meeting specific requirement, say I would like to calculate the average of score for all users greater than 20 day by day. My thought is that firstly do the

Get index of given element in array field in MongoDB

北城余情 提交于 2019-12-10 13:22:13
问题 Think of this MongoDB document: {_id:123, "food":[ "apple", "banana", "mango" ]} Question: How to get the position of mango in food? The query should return 2 in above, and don't return the whole document. Please kindly show the working query. 回答1: Starting from MongoDB version 3.4 we can use the $indexOfArray operator to return the index at which a given element can be found in the array. $indexOfArray takes three arguments. The first is the name of the array field prefixed with $ sign. The

To find square area data from mongodb Query using 2 google map point (latitude and longitude)

允我心安 提交于 2019-12-10 12:25:22
问题 I am working on mongodb Geospatial feature. Following is my db schema { _id:5c6fea5aa7f54038cc63db92, location:{ coordinates:[-96.81916,33.07516], type:"Point" }, userId:1 } Indexing on location field with 2dsphere I have 2 point and each one have latitude and longitude and its example is below sw_latitude : 32.76286484066411 sw_longitude : -96.91870791625979 ne_latitude : 32.789133220783015 ne_longitude : -96.67529208374026 base on these 2 points i need to get all userId which are cover in

Sum nested array in node.js mongodb

眉间皱痕 提交于 2019-12-10 12:08:29
问题 I have a schema in mongodb which looks like this. first_level:[{ first_item : String, second_level:[{ second_item: String, third_level:[{ third_item :String, forth_level :[{//4th level price : Number, // 5th level sales_date : Date, quantity_sold : Number }] }] }] }] 1). I want to add quantity_sold based on matching criteria in first_item, second_item, third_item and sales_date 2). I also want to find average of all the quantity_sold in a specific date. 3). I also want to find average of all