mongodb-query

MongoDB: find execution time for count() command on millions of records in a collection?

自闭症网瘾萝莉.ら 提交于 2019-12-24 13:52:29
问题 I am trying to find time required to perform a count() on a collection which is consisting of millions of testdata records, with following scenario:- 1) From 1st Mongo shell I am inserting millions of records into collection using a code for (var i = 0; i < 10000000; ++i){ db.unicorns.insert({name: 'sampleName', gender: 'm', weight: '440' }); } 2) From 2ndMongo shell I am trying to find count() on that collection( Imp: while insertion is still getting executed on 1st Mongo Shell) db.unicorns

perform math in searchqueries mongodb

流过昼夜 提交于 2019-12-24 13:52:14
问题 Is there a way to perform math in the find function as is possible in MySQL? I have the following sql query for performing searches based on radius in MySQL: SELECT id,(3959 * acos(cos(radians(37)) * cos(radians(lat)) * cos( radians(lng) - radians(-122)) + sin(radians(37)) * sin(radians(lat)))) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; And would like to be able to perform something similar in mongodb or node.js. Is this possible? 回答1: The MongoDB query

Querying nested in mongoDB

和自甴很熟 提交于 2019-12-24 13:35:28
问题 i'm learning MongoDB for a few weeks now and i have still no idea how to query nested documents in my project. I read the MongoDB-docs and googled a lot, but i found no good explanations or tutorials for my problem. Maybe you can help me! I have a Collection with the following structure (here the JSON output): { community: "1", date: 1365680790125, isIndoor: "true", party: { teams: [ { isWinner: true, players: [ { name: "jim", hits: 4, isFinisher: true }, { name: "john", hits: 6, isFinisher:

Failed Aggregation on Tornado/Motor: yielded unknown object MotorAggregationCursor

时光总嘲笑我的痴心妄想 提交于 2019-12-24 12:47:37
问题 I'm having issue to execute MongoDB aggregation operation on Tornado. This is the code, pipeline = [ {'$match': { '$or': [ {'owner.id': '56dfdaa4082024b9384c0055'}, {'members.top.member.id':'56dfdaa4082024b9384c0055'} ] }}, {'$sort': {'date_s': -1}}, {'$skip': 0}, {'$limit': 20}, {'$project':{ 'created_at': 1, 'name': 1, 'id': '$_id', 'group.group_id': 1, '_id': 0, 'permission': 1, 'owner': 1, 'type': 1, 'members.total': 1, 'desc': 1, 'declared': 1 }} ] cursor = yield db.activities.aggregate

Mongoose select with populate not working

↘锁芯ラ 提交于 2019-12-24 12:25:41
问题 I am making a query in mogoose and if i add any parameter on a query select, the populate parameter goes missing for example i have the following schemas: Department: var mongoose = require('mongoose'); var schema = mongoose.Schema({ name: {type:String,required: true,index: {unique: true}} , text: String }) module.exports=mongoose.model('Department',schema); Employee: var mongoose = require('mongoose'); var ObjectId=mongoose.Schema.ObjectId; var schema = mongoose.Schema({ name: {type:String

$sum using Mongo DB aggreagation

白昼怎懂夜的黑 提交于 2019-12-24 12:18:26
问题 I have a document as Follows: {_id:1,"$field1":10,"$field2":20} Through mongo db java driver, can we update all the documents with a new field called $total which would sum of field1 and field2 THe equivalent in Relation database would like Update table set total=field1+field2 Is the same possible using mongo DB java driver without fetching records one by one. I have tried the following link and it doesnt give the result as specified in the link https://docs.mongodb.org/manual/reference

$add with some fields as Null returning sum value as Null

喜你入骨 提交于 2019-12-24 11:52:05
问题 These are the documents in my collection { "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10} { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } Only Document 1 has a field extra marks: Now i have to make a projection as a sum of "final"+"midterm"+"extraMarks" I have written a query for projection as follows: db.students

$add with some fields as Null returning sum value as Null

孤街浪徒 提交于 2019-12-24 11:48:29
问题 These are the documents in my collection { "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10} { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } Only Document 1 has a field extra marks: Now i have to make a projection as a sum of "final"+"midterm"+"extraMarks" I have written a query for projection as follows: db.students

Get Count of specific field mongodb

北城余情 提交于 2019-12-24 11:43:11
问题 I am using below query to get combined data from users and project collections: db.collection.aggregate([ { "$group": { "_id": "$userId", "projectId": { "$push": "$projectId" } } }, { "$lookup": { "from": "users", "let": { "userId": "$_id" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$_id", "$$userId" ] }}}, { "$project": { "firstName": 1 }} ], "as": "user" } }, { "$unwind": "$user" }, { "$lookup": { "from": "projects", "let": { "projectId": "$projectId" }, "pipeline": [ { "$match": {

How does the $inc modifier work with concurrent requests in mongodb?

≯℡__Kan透↙ 提交于 2019-12-24 10:38:55
问题 The $inc modifier can be used to increment a field (such as a counter for analytics, page views etc). How does it work if there are concurrent requests ? Say I have the document: { "views" : 1 } What happens if there are two concurrent requests using $inc ? Will views be 2 or 3 ? 回答1: It's a bit broad really but I can offer the broad strokes. As of MongoDB uses collection level locking with the default storage engine. There is also the WiredTiger storage engine available which implements