mongodb-query

convert mysql query contains sum and group_concat to mongodb query

回眸只為那壹抹淺笑 提交于 2019-12-12 15:11:43
问题 I would like to convert below mysql query to mongodb query. SELECT substring(o.schedule_datetime,1,4) 'Year', SUM(IF(o.order_status in ('SUCCESS','#SUCCESS'),1,0)) 'SUCCESS' FROM ( select group_concat(distinct ifnull(os.order_status,'') order by os.order_status separator '#') 'order_status',schedule_datetime from order_summary os group by order_number )o group by 1 desc; For Example: I have sample table id order_number product_number order_status schedule_datetime 1 001 001.1 SUCCESS 20180103

mongoDb $in with aggregate query

╄→гoц情女王★ 提交于 2019-12-12 14:08:48
问题 How do I write an $in query along with aggregate in mongoDB? I want to write an equivalent mongoDB query for the SQL below SELECT name,count(something) from collection1 where name in (<<list of Array>>) and cond1 = 'false' group by name 回答1: The equivalent mongo query follows: db.collection1.aggregate([ { "$match": { "name": { "$in": arrayList }, "cond1": "false" } }, { "$group": { "_id": "$name", "count": { "$sum": "$something" } } } ]) 来源: https://stackoverflow.com/questions/41034143

Get the 2 alphabetically first documents by type

£可爱£侵袭症+ 提交于 2019-12-12 14:04:34
问题 Lets say I have the following collection structure: {type: 1, value: "f"}, {type: 2, value: "c"}, {type: 2, value: "b"}, {type: 1, value: "d"}, {type: 1, value: "e"}, {type: 2, value: "a"} Now I want to get the 2 alphabetically first documents ("value") for each "type", the result should look like: {type: 1, value: "d"}, {type: 1, value: "e"}, {type: 2, value: "a"}, {type: 2, value: "b"}, With MongoDB I have to retrieve all documents and drop the unnecessary ones. Is there another NoSQL

Unable to update the data in mongodb

拥有回忆 提交于 2019-12-12 13:11:42
问题 I need to update some fields i am using mongoose driver and express js schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ProfilesSchema = new Schema({ presentRound: { type: Number, default: 1 }, scheduleInterviewStatus: { type: Boolean, default: false }, interviewStatus: String, ratings: [{ round: Number, rating: Number, feedback: String, interviewer: String, roundStatus: String }] }); module.exports = mongoose.model('Profiles', ProfilesSchema); so in these i need to

Upsert Document and/or add a Sub-Document

百般思念 提交于 2019-12-12 12:40:19
问题 I've been wrestling with the asynchronous nature of MongoDB, Mongoose and JavaScript and how to best make multiple updates to a collection. I have an Excel sheet of client and contact data. There are some clients with multiple contacts, one per line, and the client data is the same (so the client name can be used as a unique key - in fact in the schema it's defined with unique: true ). The logic I want to achieve is: Search the Client collection for the client with clientName as the key If a

Find max element inside an array

女生的网名这么多〃 提交于 2019-12-12 12:34:08
问题 REF: MongoDB Document from array with field value max Answers in Finding highest value from sub-arrays in documents and MongoDB find by max value in array of documents suggest to use sort + limit(1), however this is really slow. Surely there is a way to use the $max operator. Suppose one gets a document like this in an aggregate match: { _id: "notImportant", array: [ { name: "Peter", age: 17 }, { name: "Carl", age: 21 }, { name: "Ben", age: 15 } ] } And you want to find the (entire, not just

MongoDB “NumberLong/$numberLong” issue while converting back to Java Object

假如想象 提交于 2019-12-12 11:27:00
问题 I am having a json which is somethink like {"Header" : {"name" : "TestData", "contactNumber" : 8019071740}} If i insert this to mongoDB it will be something like {"_id" : ObjectId("58b7e55097989619e4ddb0bb"),"Header" : {"name" : "TestData","contactNumber" : NumberLong(8019071743)} When i read this data back and try to convert to java object using Gson it throws exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a long but was BEGIN_OBJECT at line 1 column

(mongoose/promises) How do you check if document was created using findOneAndUpdate with upsert

守給你的承諾、 提交于 2019-12-12 11:14:44
问题 Consider this code, where I need to either create or update a particular document. Inbox.model.findOneAndUpdate({ number: req.phone.number }, { number: req.phone.number, country: req.phone.country, token: hat(), appInstalled: true }, { new: true, upsert: true }).then(function(inbox){ /* do something here with inbox, but only if the inbox was created (not updated) */ }); Does mongoose have a facility to be able to make the distinction between whether or not the documented was created or

Converting mongo array to object with key-value pair

我的梦境 提交于 2019-12-12 10:53:39
问题 I have a mongo document that contains an array of Strings, and I need to convert this particular array of strings into an array of object containing a key-value pair. Below is my curent appraoch to it. { "_id" : ObjectId("57e3720836e36f63695a2ef2"), "platform" : "A1", "available" : { "Community" : { "attributes" : { "type" : { "values" : [ "well-known", "simple", "complex" ], "defaultValue" : "well-known" }, [......] } Current Query: templateAttributes.find({platform:"V1"}).map(function(c){ /

Why MongoDB different query plans show different nReturned value?

左心房为你撑大大i 提交于 2019-12-12 10:13:59
问题 I have a collection faults in my MongoDB database which every document has these fields: rack_name , timestamp Just for sake of testing and comparing performances, I have created these two indexes: rack -> {'rack_name': 1} and time -> {'timestamp': 1} Now I executed the following query with explain(): db.faults.find({ 'rack_name': { $in: [ 'providence1', 'helena2' ] }, 'timestamp': { $gt: 1501548359000 } }) .explain('allPlansExecution') and here is the result: { "queryPlanner" : {