mongodb-query

MongoDB - Index not being used when sorting and limiting on ranged query

送分小仙女□ 提交于 2019-12-08 02:37:51
问题 I'm trying to get a sorted list of items using a ranged query on a collection containing bulletin-board data. The data structure of a "thread" document is: { "_id" : ObjectId("5a779b47f4fa72412126526a"), "title" : "necessitatibus tincidunt libris assueverit", "content" : "Corrumpitvenenatis cubilia adipiscing sollicitudin", "flagged" : false, "locked" : false, "sticky" : false, "lastPostAt" : ISODate("2018-02-05T06:35:24.656Z"), "postCount" : 42, "user" : ObjectId("5a779b46f4fa72412126525a"),

mongodb wildcard match all values for specific key [duplicate]

烈酒焚心 提交于 2019-12-08 02:29:05
问题 This question already has answers here : Check that Field Exists with MongoDB (4 answers) Closed 8 months ago . I am trying to figure out how to match a key and return all the values for that key. Is it possible to give the value as a wildcard? I want to return everything for that specific key using wildcard on the value. db.collection.find({"key" :"*"}) Also I was hoping this would return the entire collection as well that had the key with the wildcard value match as well. 回答1: You may be

MongoDB dateDiff between multiple documents

十年热恋 提交于 2019-12-08 02:28:40
问题 I have collection in my mongoDB which stores service given to customer along with their email address something like below { "_id" : ObjectId("56a84627f8fd4a136c0e944a"), "Vehicle" : "Honda", "ServiceSelected" : "FULL SERVICE", "FullName" : "xyz", "Email" : "xyz@xyz.com", "BookingTime" : ISODate("2015-12-27T06:00:00.000Z") }, { "_id" : ObjectId("56a84627f8fd4a136c0e944b"), "Vehicle" : "AUDI", "ServiceSelected" : "FLAT TYRE", "FullName" : "abc", "Email" : "abc@abc.com", "BookingTime" : ISODate

MongoDB $group and explicit group formation with computed column

♀尐吖头ヾ 提交于 2019-12-08 02:11:39
问题 Consider this code in SQL that forms groups based on a range of salary: ;With TableWithComputedColumn as ( select computedcolumn = Case when Salary<30000 then 'Under 30K' when Salary>30000 and Salary<60000 then '30K-60K' else 'Above 60K' end from Tbl_UserMaster ) select computedcolumn, COUNT(*) from TableWithComputedColumn group by computedcolumn I want to do this in Mongo.. My guess is that the CTE part will require me to first output {computed column} to a temporary collection and then do a

How to select where sum of fields is greater than a value in MongoDB

落花浮王杯 提交于 2019-12-08 00:53:32
问题 Using MongoDB, How would I write this regular SQL statement? SELECT * FROM table WHERE (field1+field2+field3) > 1 I've been messing with $group, $project, $add, etc. I feel like I'm dancing all around the solution but can't figure it out. 回答1: The easiest way to do this is by using $where (I am not telling that it is not possible to do this with aggregation) db.table.find({$where: function() { return this.field1 + this.field2 + this.field3 > 1 // most probably you have to handle additional

Mongo and Node.js: Finding a document by _id using a UUID (GUID)

血红的双手。 提交于 2019-12-08 00:33:33
问题 I'm developing a restAPI using node.js and i'm trying to query a mongo collection. I can query using strings (for example "companyname") but I need to be able to query the "_id" element in the document. In mongo the _id is currently stored like this (as a GUID): { "_id" : new BinData(3, "MH+t3q6PD0SxVR5z7/pzfw=="), "companyname" : "TestCompany", "databasename" : "TestDataBase", } This is what my current GET method looks like: exports.getBusinessCardData = function(req, res) { var id = req

Combining multiple sub-documents into a new doc in mongo

老子叫甜甜 提交于 2019-12-08 00:04:10
问题 I am trying to query multiple sub-documents in MongoDB and return as a single doc. I think the aggregation framework is the way to go, but, can't see to get it exactly right. Take the following docs: { "board_id": "1", "hosts": [{ "name": "bob", "ip": "10.1.2.3" }, { "name": "tom", "ip": "10.1.2.4" }] } { "board_id": "2", "hosts": [{ "name": "mickey", "ip": "10.2.2.3" }, { "name": "mouse", "ip": "10.2.2.4" }] } { "board_id": "3", "hosts": [{ "name": "pavel", "ip": "10.3.2.3" }, { "name":

How to perform mass inserts into mongodb using NodeJS

半腔热情 提交于 2019-12-07 23:15:58
问题 I Have to Insert about 10,00000 documents in mongodb using nodejs. I'm generating these documents using a for loop storing them into an array before finally inserting them into mongodb. var codeArray = new Array(); for (var i = 0; i<1000000; i++){ var token = strNpm.generate(); var now = moment().format('YYYYMMDD hhmmss'); var doc1 = {id:token, Discount_strId:"pending", Promotion_strCode:token, Promotion_strStatus:"I", Promotion_dtmGeneratedDate:now, User_strLogin:"test", Promotion_strMode:"S

MongoDB How to Query with the $date operator?

心已入冬 提交于 2019-12-07 22:07:05
问题 Edit - Context: I'm using the Talend ETL tool and using ISODate or Date or new Date in the query like the following fail with an error, so I need a work around: {'dt' : ISODate('2014-01-01') } {'dt' : Date('2014-01-01') } {'dt' : new Date('2014-01-01') } I cannot do so without the following error: at com.mongodb.util.JSONParser.read(JSON.java:272) at com.mongodb.util.JSONParser.parse(JSON.java:161) at com.mongodb.util.JSONParser.parseObject(JSON.java:231) at com.mongodb.util.JSONParser.parse

mongodb array matching

感情迁移 提交于 2019-12-07 21:12:29
问题 How do I accomplish the following? db.test.save( {a: [1,2,3]} ); db.test.find( {a: [1,2,3,4]} ); //must match because it contains all required values [1, 2, 3] db.test.find( {a: [1,2]} ); //must NOT match because the required value 3 is missing I know about $in and $all but they work differently. 回答1: Interesting..The problem is..the $in and the $or operators get applied on the elements of the array that you are comparing against each document in the collection, not on the elements of the