mongodb-query

How to sort sub-documents in the array field?

随声附和 提交于 2019-12-29 09:14:25
问题 I'm using the MongoDB shell to fetch some results, ordered. Here's a sampler, { "_id" : "32022", "topics" : [ { "weight" : 281.58551703724993, "words" : "some words" }, { "weight" : 286.6695125796183, "words" : "some more words" }, { "weight" : 289.8354232846977, "words" : "wowz even more wordz" }, { "weight" : 305.70093587160807, "words" : "WORDZ" }] } what I want to get is, same structure, but ordered by "topics" : [] { "_id" : "32022", "topics" : [ { "weight" : 305.70093587160807, "words"

How can i calculate price positive and negative price using mongodb or robomongo?

▼魔方 西西 提交于 2019-12-29 08:24:16
问题 below is my userpricing collection data { "_id" : ObjectId("584bc9ba420a6b189c510af6"), "user_id" : 1, "mobilenumber":"01234", "price" : 2000.0, "type" : "credit", }, { "_id" : ObjectId("584bc9ba420a6b189c510af6"), "user_id" : 1, "mobilenumber":"01234", "price" : -1000.0, "type" : "credit", }, { "_id" : ObjectId("584bc9ba420a6b189c3323w23"), "user_id" : 2, "mobilenumber":"04321", "price" : 1000.0, "type" : "credit", } here i want to calculate total postive and total negative price of all user

Combine full text with other index

纵饮孤独 提交于 2019-12-29 08:24:08
问题 I have a full text index and an index on a created date. My query on the date alone returns a nice, small 44 records very quickly (within a second): > db.oneMillionDocumentsIndexed.count({created: {$lte: ISODate("2016-02-06T15:34:59.019Z")} }) 44 However, if I combine this with the text search the query is incredibly slow: > db.oneMillionDocumentsIndexed.count({ created: {$lte: ISODate("2016-02-06T15:34:59.019Z")}, $text: { $search: "raven" } }) It appears to use both indexes: { "queryPlanner

MongoDB Show children items in One to Many relationship

断了今生、忘了曾经 提交于 2019-12-29 08:15:20
问题 This is the example of 1: N relationship. There is a one root item which consists of the few items (children): { "_id" : ObjectId("52d017d4b60fb046cdaf4851"), "dates" : [ 1399518702000, 1399126333000, 1399209192000, 1399027545000 ], "dress_number" : "4", "name" : "J. Evans", "numbers" : [ "5982", "5983", "5984", "5985" ], "goals": [ "1", "0", "4", "2" ], "durations": [ "78", "45", "90", "90" ] } What I want to do is to show children data from the root item: { "dates": "1399518702000",

Mongo Sort by Count of Matches in Array

怎甘沉沦 提交于 2019-12-29 08:11:48
问题 Lets say my test data is db.multiArr.insert({"ID" : "fruit1","Keys" : ["apple", "orange", "banana"]}) db.multiArr.insert({"ID" : "fruit2","Keys" : ["apple", "carrot", "banana"]}) to get individual fruit like carrot i do db.multiArr.find({'Keys':{$in:['carrot']}}) when i do an or query for orange and banana, i see both the records fruit1 and then fruit2 db.multiArr.find({ $or: [{'Keys':{$in:['carrot']}}, {'Keys':{$in:['banana']}}]}) Result of the output should be fruit2 and then fruit1,

Getting unix timestamp in seconds out of MongoDB ISODate during aggregation

给你一囗甜甜゛ 提交于 2019-12-29 05:34:09
问题 I was searching for this one but I couldn't find anything useful to solve my case. What I want is to get the unix timestamp in seconds out of MongoDB ISODate during aggregation. The problem is that I can get the timestamp out of ISODate but it's in milliseconds. So I would need to cut out those milliseconds. What I've tried is: > db.data.aggregate([ {$match: {dt:2}}, {$project: {timestamp: {$concat: [{$substr: ["$md", 0, -1]}, '01', {$substr: ["$id", 0, -1]}]}}} ]) As you can see I'm trying

Find last record of each day

孤街醉人 提交于 2019-12-29 01:19:16
问题 I use mongodb (I'm new in mongodb) to store data about my power consumption , each minute there is a new record, here is an example : {"id":"5309d4cae4b0fbd904cc00e1","adco":"O","hchc":7267599,"hchp":10805900,"hhphc":"g","ptec":"c","iinst":13,"papp":3010,"imax":58,"optarif":"s","isousc":60,"motdetat":"Á","date":1393156826114} so I have around 1440 records a day. I want to compute the cost by day, but the problem for this is that I need the last record of the day, because this record can give

Mongodb concat int and string

我与影子孤独终老i 提交于 2019-12-28 16:37:37
问题 I'm trying to project FileName and FileSize for all my files in my collection with a size of 50 mb and greater, but I cannot concat a the type FileSize as it has a type of Int I want the projection to be { "result" : [ { "_id" : ObjectId("5652c399a21dad0bb01b6308"), "FileName" : "1234567890.xml", "FileSize" : "11.06 MB" }, { "_id" : ObjectId("5652c399a21dad0bb01b630f"), "FileName" : "2468101214.xml", "FileSize" : "320.48 MB" }, { "_id" : ObjectId("5652c399a21dad0bb01b631f"), "FileName" :

Mongodb aggregation pipeline how to limit a group push

故事扮演 提交于 2019-12-28 03:05:50
问题 I am not able to limit the amount of pushed elements in a group function with aggregation pipeline. Is this possible? Small example: Data: [ { "submitted": date, "loc": { "lng": 13.739251, "lat": 51.049893 }, "name": "first", "preview": "my first" }, { "submitted": date, "loc": { "lng": 13.639241, "lat": 51.149883 }, "name": "second", "preview": "my second" }, { "submitted": date, "loc": { "lng": 13.715422, "lat": 51.056384 }, "name": "nearpoint2", "preview": "my nearpoint2" } ] Here is my

using $slice operator to get last element of array in mongodb

只愿长相守 提交于 2019-12-28 02:07:08
问题 How to get last element of array with conditions in mongodb. I am unable to use slice. Here is my code { "1" : { "relevancy" : [ "Y" ] }, "_id" : ObjectId("530824b95f44eac1068b45c0") } { "1" : { "relevancy" : [ "Y", "Y" ] }, "_id" : ObjectId("530824b95f44eac1068b45c2") } { "1" : { "relevancy" : [ "N" ] }, "_id" : ObjectId("530824b95f44eac1068b45c3") } { "1" : { "relevancy" : [ "Y", "Y" ] }, "_id" : ObjectId("530824b95f44eac1068b45c4") } { "1" : { "relevancy" : [ "Y", "N" ] }, "_id" : ObjectId