mongodb-query

MongoDB: How to find nth highest salary from collection

十年热恋 提交于 2019-12-08 14:21:41
问题 I need to find the nth highest salary in a mongodb from Employees collection. also would be really helpful if someone could gimme an idea of applying joins in mongodb. 回答1: This should work db.Employees.find({}).sort({"Emp salary":-1}).limit(1) //for first highest salary db.Employees.find({}).sort({"Emp salary":-1}).skip(1).limit(1) // for second highest salary Similarly you can do db.Employees.find({}).sort({"Emp salary":-1}).skip(nthVarible - 1).limit(1) . 回答2: Try this out: db.salary.find(

MongoDb insert or add in embedded document

ぐ巨炮叔叔 提交于 2019-12-08 13:13:24
问题 This is my final document structure. I am on mongo 3.0 . { _id: "1", stockA: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ], stockB: [ { size: "S", qty: 27 }, { size: "M", qty: 58 } ] } I am randomly adding elements inside stockA or stockB collections. I would like come up with a query which would satisfy following rules: If item is not exist - create whole document and insert item in stock A or B collection. If item is exist but stock A or B collection is missing create stock collection

inserting image into mongo from java result in a strange error

萝らか妹 提交于 2019-12-08 12:38:51
问题 I have the following code for saving image in mongodb: public static void insertImage() throws Exception { String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.save(); } And I got this from this link: link for code But when I use that I get the following error and I do not know how to fix it ... Can anyone help

Do geospatial queries work on arrays? ( $geoWithin, $geoIntersects )

安稳与你 提交于 2019-12-08 12:31:05
问题 I have documents as the one below stored in MongoDB 3.0.5 { "_id" : ObjectId("55f3a6ae0907233b5e9e7da0"), "user" : { "id" : "2d5b093ec8a3", "regions" : [ { "id" : NumberLong(1442365443852), "name" : "penta", "type" : "Polygon", "center" : { "lat" : -12.1254159880008100, "lng" : -77.0316830277442930 }, "zoom" : 17.0000000000000000, "coordinates" : [ [ -77.0322804898023610, -12.1271067552781560 ], [ -77.0336792618036270, -12.1255133434450870 ], [ -77.0326449349522590, -12.1239143495252150 ], [

find and count in single mongodb query

狂风中的少年 提交于 2019-12-08 12:18:42
问题 My documents looks like this. { "_id" : ObjectId("572c4bffd073dd581edae045"), "name" : "What's New in PHP 7", "description" : "PHP 7 is the first new major version number of PHP since 2004. This course shows what's new, and what's changed.", "difficulty_level" : "Beginner", "type" : "Normal", "tagged_skills" : [ { "_id" : "5714e894e09a0f7d804b2254", "name" : "PHP" } ], "created_at" : 1462520831.649, "updated_at" : 1468233074.243 } Is it possible to get recent 5 documents and total count in a

MongoDB aggregation pipeline

孤街浪徒 提交于 2019-12-08 11:39:31
问题 I have this type of documents: collection:People {name:"George", grade:5, school:"MathHighSchool"} and many more examples. I need a query that finds all people who: study in the MathHighSchool (so we have db.people.aggregate({$match:{school:"MathHighSchool"}},.... ) and then group them by their grades, as it shows the number of people with grade <3 number of people with grade between 3 and 5 and number of people with grade > 5. Any ideas? 回答1: In order to conditionally sum matches in your

Mongodb: Group by element and show the sub-document count based on condition and sort the document by date

眉间皱痕 提交于 2019-12-08 11:35:05
问题 I've collections of documents as like as below: { "_id" : ObjectId("55d4410544c96d6f6578f893"), "executionProject" : "Project1", "suiteList" : [ { "suiteStatus" : "PASS", } ], "runEndTime" : ISODate("2015-08-19T08:40:47.049Z"), "runStartTime" : ISODate("2015-08-19T08:40:37.621Z"), "runStatus" : "PASS", "__v" : 1 } { "_id" : ObjectId("55d44eb4c0422e7b8bffe76b"), "executionProject" : "Project1", "suiteList" : [ { "suiteStatus" : "PASS", } ], "runEndTime" : ISODate("2015-08-19T09:39:13.528Z"),

Mongoose query to exclude a value from the result

半城伤御伤魂 提交于 2019-12-08 09:36:35
问题 I want to retrieve a users list but excluding the current user ID. Right now I have it without the exclusion: User.find() .or([ {email: new RegExp(req.param('searchTerm'), 'i')}, {displayName: new RegExp(req.param('searchTerm'), 'i')} ], 'displayName email profileImageURL') .limit(20) .exec(function(err, users) How can I change the query to use the AND I need to exclude the current userID? Thanks 回答1: Add an initial where call to your query chain for that: User.find() .where('_id').ne(userID)

MongoDB 3-level aggregation

不问归期 提交于 2019-12-08 08:56:37
问题 Suppose we have a list of schools so that each school is assigned city, state (province) and country. Is it possible to build an aggregated list of unique cities/states/countries with MongoDB aggregation framework? What I have: [ { "School_name” : "School #1", "state" : "CA" "city” : "San-Francisco", "country” : "USA", }, { "School_name” : "School #2", "state" : "CA" "city” : "San-Francisco", "country” : "USA", }, { "School_name” : "School #3", "state" : "CA" "city” : "Los Angeles", "country”

Mongoose: Filter collection based on values in another collection

扶醉桌前 提交于 2019-12-08 08:41:30
Consider documents that contain an arrays of ID of another collection, how can I find documents applying a filter based on the related collection using mongoose? I can't find my error Installation.aggregate( [ // Stage 1 { $lookup: { from: "users", localField: "_id", foreignField: "_id", as: "Users" } }, // Stage 2 { $unwind: { path: "$Users" } }, // Stage 3 { $match: {"Users.Email1" : "test@test.com"} }, { $sort: { _id: -1 } }, { $limit: 10 } ] ,function (err, installations) { console.log(installations); //<<<<< Empty /* Installation.populate( 'Parent', '_id Email1','Company'), function(err