mongodb-query

Sorting mongodb client findOne() in node

拟墨画扇 提交于 2019-12-23 22:12:07
问题 I am building a server-less app in AWS with Lambda and Node.js. I currently have a MongoDB out at mLab. I am attempting to get the "latest" record based on an ISODate string. Using either findOne() or find w/limit 1 it returns the same record everytime (not the latest one). I have 2 records in my test table that look like: { "field1": "myField", "versionTimestamp": "2017-06-13T18:33:06.223Z" } { "field1": "myField", "versionTimestamp": "2017-12-13T18:33:06.223Z" } No matter what I do it

how to retrieve partial objects from object array in a field in mongodb

回眸只為那壹抹淺笑 提交于 2019-12-23 20:55:06
问题 I have a mongoose schema like - db.foo.insert({one:'a',friends:[{two:'b',three:'c'},{two:'d',three:'e'},{two:'f',three:'G'}]}) now what i want is two retrieve only the 'two' part of friends array that is I want to find an array of all the values of two in each object in friends array Is such a projection possible in mongodb where in the output looks like - ['b','d','f'] 回答1: aggregate is your answer db.foo.aggregate({"$project" : {"two" : "$friends.two"}}).result there is another way to do

Using aggregate $lookup and $mergeObjects

江枫思渺然 提交于 2019-12-23 19:07:21
问题 I want to join collection. before, I used only lookup, so that I could get separated field that is joined. but I need to get result similar mysql join. I noticed there is $lookup and $mergeObjects for this action but not working well. user collection model. { "_id": ObjectId("xxxxxxx"), //this is default id from mongoDB "name": 'admin user', "email": 'admin@test.com', "password": 'xxxxxxxx', "roles": [ { "id": 0, "approved": true },{ "id": 2, "approved": true } ] },{ "_id": ObjectId("xxxxxxx"

Retrieve index of item in array in MongoDB

…衆ロ難τιáo~ 提交于 2019-12-23 19:04:37
问题 similar to -Aggregation: add option to $unwind to emit array index -Get index of an item within mongodb query I have this use case. Tastiest Fruit Rankings: {"date": "Jan 1st", "fruit_ranking": ["Apple", "Orange", "Grape", "Kiwi", "Mango", "Pear"]}, {"date": "Jan 2nd", "fruit_ranking": ["Orange", "Grape", "Kiwi", "Pear", "Apple"]} ..... {"date": "Dec 31st", "fruit_ranking": ["Kiwi", "Apple", "Grape", "Mango", "Pear"]} I'm trying to grab the ranking of "Pear" for each day Jan 1st - Dec 31st

Calculate a score from an existing fields with conditions

a 夏天 提交于 2019-12-23 17:52:38
问题 I'm working on MongoDB 2.6.9 and NodeJs 0.10.37 and I have a collection vols which means flights. > db.vols.findOne() { "_id" : ObjectId("5717a5d4578f3f2556f300f2"), "Orig" : "AGP", "Dest" : "OTP", "Flight" : 126, "Routing" : "AGP-OTP", "Stops" : 0, "Seats" : 169, "Ops_Week" : 3, "Eff_Date" : "2016-04-14", "Mkt_Al" : "0B", "Dep_Time" : 1110, "Thru_Point" : "", "Arr_Time" : 1600, "Block_Mins" : 230 } Each document refers to one flight done by an Airline Company and it gives details, for

Is there any way to use $filter and $sum in the same $project?

末鹿安然 提交于 2019-12-23 17:43:53
问题 Let's say I have a document in my aggregate pipeline that looks like this: { scores: [ {type: 'quiz', score: 75}, {type: 'quiz', score: 62}, {type: 'final', score: 34}, ] } I'm using $project to transform it, and I'd like to get the sum of the the quiz scores, is there a way I could somehow chain my $filter and $sum . I know that I could potentially use two $project s, but the way I currently have my pipeline set up would force me to continue reproject a ton of keys in my second project,

MongoDB: groupby subdocument and count + add total count

痞子三分冷 提交于 2019-12-23 17:42:02
问题 What I want to achieve Suppose having the following subdocument: { "id":1, "url":"mysite.com", "views": [ {"ip":"1.1.1.1","date":"01-01-2015"}, {"ip":"2.2.2.2","date":"01-01-2015"}, {"ip":"1.1.1.1","date":"01-01-2015"}, {"ip":"1.1.1.1","date":"01-01-2015"} ] } I want to count: how many IPs there are based on the "ip" value and also count the total of subdocuments in "views" if possible in the same query , to achieve the following result: [ { "_id":"2.2.2.2", "count":1 }, { "_id":"1.1.1.1",

Meteor $and with $or

僤鯓⒐⒋嵵緔 提交于 2019-12-23 17:23:38
问题 I'm trying to do an $and and then $or in Meteor for my mongo query I have the following but it doesn't seem to be working Would like the query to match documents where organizationId key has value in the variable user.organizationId AND where the type key is either 'converntional' or 'transition' { organizationId: user.organizationId, $and:[ { $or:[ {type: 'conventional'}, {type: 'transition'} ]} ] }; I can't use $not as I'm pretty sure it's not supported in Meteor. Right now the package I'm

Find all documents that share max(value) found in aggregate step

随声附和 提交于 2019-12-23 16:23:59
问题 I'm trying to find all documents that share the max(num_sold). I cant just sort descending and do a limit(1) because I'd miss the case where other docs also have the same max(num_sold). Given this over-simplified dataset: {"item":"Apple", "num_sold": 49} {"item":"Orange", "num_sold": 55} {"item":"Peach", "num_sold": 55} {"item":"Grape", "num_sold": 20} {"item":"Banana", "num_sold": 20} I want to get back {"item":"Orange", "num_sold": 55} {"item":"Peach", "num_sold": 55} Using SQL, this is a

how can i find text search in an array in mongodb

让人想犯罪 __ 提交于 2019-12-23 16:18:09
问题 { "_id":objectId(23651478), "name":"Tomatos" "array":[ {"title":"Vegetables"} ] "description":"Vegitables are good to health" }, { "_id":objectId(45761244), "name":"Apples" "array":[ {"title":"Fruits"} ] "description":"Fruits are good to health, vegitables are also good to health" }, { "_id":objectId(45761244), "name":"Apples" "array":[ {"title":"Vegetables-home-made"} ] "description":"Fruits are good to health, vegitables are also good to health" } Above is my mongo schema modal for my