mongodb-query

Add Unique only to array and keep field count on update

♀尐吖头ヾ 提交于 2019-12-13 02:05:57
问题 I store in a collection an array of item (stringId). All element in this array must be unique. So I use $addToSet to push my item. But, I also would like to set in the same request the size of my array in a field : { unique_array: ['12', '20', '18'], size_of_array: 3 } => Add to set 15 { unique_array: ['12', '20', '18', '15'], => Add to set size_of_array: 4 => Incremented } => Add to set 18 { unique_array: ['12', '20', '18', '15'], => Already in the set size_of_array: 4 => Not incremented }

mongo find query on joda datetime

微笑、不失礼 提交于 2019-12-13 01:27:05
问题 i am trying to use find query in mongodb to determine the records on that specific date , the query is working fine when i pass it the normal date object and if i find according to dateCreated field, but for joda date field, i don't know how should i form a joda date right now i am using this query for normal records var from = new Date('2015-05-18T00:00:00.000Z'); var to = new Date('2016-05-19T00:00:00.000Z'); db.delivery.find( {"dateCreated" : { $gte:from,$lt:to } } ); now i also have a

How to subtract in mongodb php

岁酱吖の 提交于 2019-12-13 01:16:54
问题 $getdataPipeline = array( array( '$match' => array( 'project_id' => array('$in' => $mysql_project_id) // Validating project ID ), '$match' => array('project_id' => $project_id) ), array( '$group' => array( '_id' => array('pro_id' => '$project_id', 'uid' => '$user_id'), "wh" => array('$subtract' => array(array('$sum' => '$toltal_timein_minutes'), array('$sum' => '$holding_durationin_minutes'))) )) ); Running query: $ValidProjectIdInMongo = $collection->aggregate($getdataPipeline); I'm getting

How to find items in a collections which are not in another collection with MongoDB

你离开我真会死。 提交于 2019-12-13 01:09:43
问题 I want to query my mongodb to perform a non-match between 2 collections. Here is my structure : CollectionA : _id, name, firstname, website_account_key, email, status CollectionB : _id, website_account_key, lifestage, category, target, flag_sistirt I'am trying to find Items in B, for which, there is no lines in A (website_account_key is unique and allows to find elements in B for each A [one-to-one]) I tried to do : dataA_ids = db.dataA.find().map(function(a){return a.website_account_key;})

Need to find the most frequently occurring value of a field in a aggregate

梦想的初衷 提交于 2019-12-13 01:04:45
问题 From a MongoDB aggregation returning a single record for each hour, I also need to know the 'mode' or most frequently occurring value in a field. So far I have selected the set of records between two dates, and am returning a single record for each hour including an average of a field value. But I also need the most frequent category where category number field containing 1,2,3 or 4. var myName = "CollectionName" //schema for mongoose var mySchema = new Schema({ dt: Date, value: Number,

mongodb c# select specific field dot notation

↘锁芯ラ 提交于 2019-12-13 00:44:03
问题 In addition for my previous question: mongodb c# select specific field. I'm writing a generic method for selecting a specific field. the requirements are: Field can be of any type Return type is T Field can be inside a sub field Field can be inside array items - in that case its OK to select the specific field of all the items in the array for shorts, im looking for the "select" / dot notation capability. for example: the wanted method: T GetFieldValue<T>(string id, string fieldName) the

Mongodb embed structure updating and searching?

安稳与你 提交于 2019-12-13 00:27:47
问题 Playing with MongoDB to store results from a nmap network scan, and trying to figure out the best way to structure the data. At the moment I have the following : { "_id" : ObjectId("525f94097025166583e18eba"), "ip" : "127.0.0.1", "services" : [ { "port" : "22", "port_info" : [ { "product" : "ssh", "version" : "1.0" } ] }, { "port" : "80", "port_info" : [ { "product" : "apache", "version" : "2.0" } ] } ] } I can find for example hosts with port 22 open, example against just 127.0.0.1 with this

Query for documents which have an internal sub-field of a given value

女生的网名这么多〃 提交于 2019-12-13 00:24:28
问题 I have documents like this one at collection x at MongoDB: { "_id" : ... "attrKeys": [ "A1", "A2" ], "attrs" : { "A1" : { "type" : "T1", "value" : "13" }, "A2" : { "type" : "T2", "value" : "14" } } } The A1 and A2 elements above are just examples: the attrs field may hold any number of keys of any name. The key names in attrs are stored in the attrNames field. I would like to query for documents which have an attr with a sub-field of a given value. For example, a query for documents that have

How to count distinct date items in a timestamp field in Mongoose/NodeJS?

孤者浪人 提交于 2019-12-13 00:21:04
问题 I use Mongoose in my NodeJS, and I have a collection with documents that look like this - var SomeSchema = new Schema({ date: { type: Date, default: new Date() }, some_item: { type: String } }); The date field contains date with the time component (for example, 2014-05-09 09:43:02.478Z ). How do I get a count of distinct items for a given date, say 2014-05-08 ? EDIT: Ideally, I would like to get a count of records for each distinct date. 回答1: What you want is usage of the aggregation

GroupBy multiple columns in MongoDB

笑着哭i 提交于 2019-12-12 23:17:11
问题 I have a shifts collection like below { "_id" : ObjectId("5885a1108c2fc432d649647d"), "from" : ISODate("2017-01-24T06:21:00.000Z"), //can be weekday, sat, sun "to" : ISODate("2017-01-24T08:21:00.000Z"), "jobId" : ObjectId("586d7d6acfc7e05669d6e2c8"), "hourlyRate" : 32 //this wil vary based on **from** field } { "_id" : ObjectId("5885a1108c2fc432d649647e"), "from" : ISODate("2017-01-25T06:21:00.000Z"), "to" : ISODate("2017-01-25T08:21:00.000Z"), "jobId" : ObjectId("586d7d6acfc7e05669d6e2c8"),