mongodb-query

How to count occurrences in nested document in mongodb?

痴心易碎 提交于 2019-12-09 07:12:59
问题 In the situation where there is an array nested within an array, how would I count the number of a specific value? For example, I want to count the number of "answers" in the document below. query should return that there are 2 apples and 1 banana. { "_id" : ObjectId("52c1d909fc7fc68ddd999a73"), "name" : "Some survey", "questions" : [ { "_id" : ObjectId("52c1e250fc7fc68ddd999a75"), "answers" : [ { "userId" : "some GUIDs", "answer" : "apple" }, { "userId" : "some GUID", "answer" : "apple" }, {

get all the documents having max value using aggregation in mongodb

旧街凉风 提交于 2019-12-09 06:25:26
问题 I want to fetch "all the documents" having highest value for specific field and than group by another field. Consider below data: _id:1, country:india, quantity:12, name:xyz _id:2, country:USA, quantity:5, name:abc _id:3, country:USA, quantity:6, name:xyz _id:4, country:india, quantity:8, name:def _id:5, country:USA, quantity:10, name:jkl _id:6, country:india, quantity:12, name:jkl Answer should be country:india max-quantity:12 name xyz name jkl country:USA max-quantity:10 name jkl I have

How to make a mutation query for inserting a list of (Array) fields in GraphQL

若如初见. 提交于 2019-12-09 04:10:16
问题 recently I started working on GraphQL, I am able to insert data in flat schema without any problem but when it comes to an Array of data I am getting an error like { "errors": [ { "message": "Must be input type" } ]} I am testing my query using postman, my mutation query is mutation M { AddEvent ( title: "Birthday event" description:"Welcome to all" media:[{url:"www.google.com", mediaType:"image" }] location:[{address:{state:"***", city:"****"}}] ) {title,description,media,location,created,

MongoDB : Is there a way to detect a value trend using aggregation?

半腔热情 提交于 2019-12-09 03:46:41
问题 I'm trying to detect the "trend" of a value in a collection. Let's say I have the following: { created_at: 2014-12-01, value:1015 } { created_at: 2014-12-01, value:1015 } { created_at: 2014-12-01, value:1019 } { created_at: 2014-12-02, value:1018 } { created_at: 2014-12-02, value:1021 } { created_at: 2014-12-03, value:1010 } { created_at: 2014-12-03, value:1012 } { created_at: 2014-12-03, value:1011 } { created_at: 2014-12-04, value:1012 } I just want to have an output like: { created_at:

Find Documents Where a Field Compares with Another in an Array

时间秒杀一切 提交于 2019-12-09 03:18:43
问题 Let's say I have a collection of documents that look like this: { "_id" : ObjectId("5afa6df3a24cdb1652632ef5"), "createdBy" : { "_id" : "59232a1a41aa651ddff0939f" }, "owner" : { "_id" : "5abc4dc0f47f732c96d84aac" }, "acl" : [ { "profile" : { "_id" : "59232a1a41aa651ddff0939f" } }, { "profile" : { "_id" : "5abc4dc0f47f732c96d84aac" } } ] } I want to find all documents where createdBy._id != owner._id , AND where the createdBy._id appears in one of the entries in the acl array. Eventually, I

How do update a specific field in mongoose?

我们两清 提交于 2019-12-08 20:25:52
问题 I have a data set which is var JobSchema = Schema({ candidates: [{ user: { type: Schema.Types.ObjectId, ref: 'User'}, status: { type: String, default: 'In Progress' }, }] }); I want to find a specific job's _id and update a specific candidates' field such as { _id: 123, user: 12345, status: "In Progress" } The url for this operation is ---> localhost:3000/apply/:job_id/user_id For example Let say that this is the current saved data in job mongodb { "_id" : 123, ---> job_id "candidates" : [{ "

How do I calculate route distance between many GeoJSON points in MongoDB?

倖福魔咒の 提交于 2019-12-08 19:44:15
问题 How do I calculate route distance between many GeoJSON points in MongoDB? Can I have a database query that sort items by their date field, then calculates the distance between points and finally sums all of then to calculate the total distance? Here are some examples of my data: { _id: 599cfc236ed0d81c98007f66 tracerId: 59a07ea26ed0d81d78001acd loc { type: "2dsphere", coordinates: [ 159.9, -37.92 ] }, date: 2017-08-26 00:16:42, speed: 58, } { _id: 59a074d46ed0d81d78001acc tracerId:

MongoDB retrieve only matching sub documents from a document with c#

纵然是瞬间 提交于 2019-12-08 19:43:33
I want the following query below to only return those sub documents from the empActivity array where the Stamp field in every sub document matches the Stamp value in the query. empId and empActivity are the outer level fields with empActivity having embedded documents. db.emp_activity.find({$and : [{"empId" : "999"}, {"empActivity.Stamp" : { $lte : ISODate("2015-01-09T12:33:39.927Z")}}]}) The problem is that it also returns all the sub documents that dont match the date in the query, apart from the 4 sub documents having a date of 9th Jan 2015, the query above also returns sub documents whose

Laravel Mongo Many To Many relation wherehas not working

北慕城南 提交于 2019-12-08 19:30:08
问题 I have two mongo documents that are related to each other in a many to many relationship. One is called Lawyer and the other LawCase. My Lawyer model has: public function cases() { return $this->belongsToMany('App\LawCase'); } My LawCase model has: public function lawyers() { return $this->belongsToMany('App\Lawyer'); } All I am trying to do is find lawyers that have a certain category of law cases. $lawyers = App\Lawyer::whereHas('cases', function($q){ $q->where('category', '=', 'DUI'); })-

'like' or $regex query inside $cond in MongoDB

試著忘記壹切 提交于 2019-12-08 15:56:29
问题 Please go through this question of mine: MongoDB $group and explicit group formation with computed column But this time, I need to compare strings, not numbers.The CASE query must have a LIKE : CASE WHEN source LIKE '%Web%' THEN 'Web' I then need to group by source. How to write this in Mongo? I am trying the following but not sure if $regex is supported inside $cond . By the way, is there a list of valid operators inside $cond somewhere? Looks like $cond isn't very fond of me :) db.Twitter