mongodb-query

Store location data in Mongodb document

◇◆丶佛笑我妖孽 提交于 2020-01-24 22:58:06
问题 In my current project , I am storing the location data in the following format in a Mongodb document "location" : { "loc" : { "lng" : -118.15592692, "lat" : 34.03566804 }, "geocode" : { "city" : "East Los Angeles", "state" : "CA", "zipcode" : "90022", "countrycode" : "US", "country" : "United States" } } There is a 2d index created on location.loc field:- { "location.loc" : "2d" } But the geospatial queries are taking long time when the result set is large i.e more than 7000 records. it seems

MongoDB aggregate count of items in two arrays across different documents?

匆匆过客 提交于 2020-01-24 17:33:07
问题 Here is my MongoDB collection schema: company: String model: String cons: [String] // array of tags that were marked as "cons" pros: [String] // array of tags that were marked as "pros" I need to aggregate it so I get the following output: [{ "_id": { "company": "Lenovo", "model": "T400" }, "tags": { tag: "SomeTag" pros: 124 // number of times, "SomeTag" tag was found in "pros" array in `Lenovo T400` cons: 345 // number of times, "SomeTag" tag was found in "cons" array in `Lenovo T400` } }...

How can transpose mongodb document list to tabular form?

試著忘記壹切 提交于 2020-01-24 14:07:04
问题 How can transpose mongodb document list to tabular form? Example: I have the following list of BsonDocuments [0] = { "_id" : ObjectId("546ce80d43f5c2b7d8a713bd"), "Address" : "61- A, Pocket- C, Ram Vihar Phase II, UP", "Pincode" : 110091, "Locality" : "Mayur Vihar Phase II", "City" : "Delhi", "Latitude" : 28.618156, "Longitude" : 76.3055877 } [1] = { "_id" : ObjectId("546ce80d43f5c2b7d8a713bd"), "Address" : "61- A, Pocket- C, Phase II, Rahi", "Pincode" : 110091, "Locality" : "Mayur Vihar

MongoDB: Find objects with field names starting with

时间秒杀一切 提交于 2020-01-24 11:11:11
问题 Query for a MongoDB: From a given collection (see example below) I need only objects listed that comprise fields, where the field name starts with "need_". Example of a collection with three objects /* 1 */ { "_id" : 1, "need_some" : "A", "need_more" : 1, "website_id" : "123456789" } /* 2 */ { "_id" : 2, "need_more" : 2, "website_id" : "123456789" } /* 3 */ { "_id" : 3, "website_id" : "123456789" } Desired Output: /* 1 */ { "_id" : 1, "need_some" : "A", "need_more" : 1, "website_id" :

Nodejs + Mongo db connect with server database with username and password

旧时模样 提交于 2020-01-24 01:35:06
问题 I am working on an existing project. Here using multiple database. I am struggling to connect the server database with username and password. Currently now I am using local database without username and password. It's working fine. I am using mongodb npm module. db.js var mongodb = require('mongodb'); module.exports.init = function (callback) { var server = new mongodbs.Server('localhost', 27017, {}) //var server = new mongodbs.Server('abc.com', 27017, username , password, {}) module.exports

How to search character by character in mongodb array text field?

时光怂恿深爱的人放手 提交于 2020-01-24 01:00:10
问题 I have documents in mongodb are like : [{ "_id" : 1, "name" : "Himanshu", "tags" : ["member", "active"] },{ "_id" : 2, "name" : "Teotia", "tags" : ["employer", "withdrawal"] },{ "_id" : 3, "name" : "John", "tags" : ["member", "deactive"] },{ "_id" : 4, "name" : "Haris", "tags" : ["employer", "action"] }] What I want to search here is if we have array of filter like {"tags" : ["member", "act"]} it will reply back id's 1 and 2 because here member is full match and act partial match in two

MongoDB get individual count from sudocuments with comparison operations

冷暖自知 提交于 2020-01-23 17:27:06
问题 I am using mongoDB I use query db.getCollection('managements').find({'managers.role':"HR"},{'managers.$':1}) I get result as below /* 1 */ { "_id" : ObjectId("1"), "managers" : [ { "id" : "100", "role" : "HR" }, { "id" : "101", "role" : "ADMIN" }, { "id" : "102", "role" : "ADMIN" } ] } /* 2 */ { "_id" : ObjectId("2"), "managers" : [ { "id" : "104", "role" : "ADMIN" }, { "id" : "105", "role" : "HR" } , { "id" : "106", "role" : "HR" } , { "id" : "107", "role" : "HR" } ] } Now I want to get

How to use $hint in MongoDB aggregation query?

余生颓废 提交于 2020-01-23 05:13:18
问题 I am using mongo v3.0.1 on a ubuntu machine. And I have a collection of 300million rows. I have created two indexes based on my query preference. When I am trying to run aggregation with explain, It is taking the inefficient index, and that is why it's taking 20-25 secs more time. Is there any way to put $hint , so that my aggregation query use the appropriate index. $match is in my first pipeline stage. I have two indexes: "Host_-1_SiteType_-1" "VisitTime_-1_AccountId_-1_Host_-1_SiteType_-1

How can we implement Pagination for Mongodb Collection using mongoTemplate

偶尔善良 提交于 2020-01-22 19:48:12
问题 I'm a noob in mongoDb i need to implement Pagination for any specific Collection for instance say I have a Collection Foo and i have a Fucntion that returns all the records from the Foo collection public List<Foo> getFoo(){ } But i need to fetch records from the Foo by implementing pagination how can i achieve this by using mongoTemplate Spring data mongodb? 回答1: For general pagination you can use the .skip() and .limit() modifiers on the Query object which you can pass in as arguments to

How to join two collections in mongoose

徘徊边缘 提交于 2020-01-22 07:41:21
问题 I have two Schema defined as below: var WorksnapsTimeEntry = BaseSchema.extend({ student: { type: Schema.ObjectId, ref: 'Student' }, timeEntries: { type: Object } }); var StudentSchema = BaseSchema.extend({ firstName: { type: String, trim: true, default: '' // validate: [validateLocalStrategyProperty, 'Please fill in your first name'] }, lastName: { type: String, trim: true, default: '' // validate: [validateLocalStrategyProperty, 'Please fill in your last name'] }, displayName: { type: