mongodb-query

Converting the date returned from mongo to this format 2015-10-25

送分小仙女□ 提交于 2019-12-12 02:55:28
问题 I have a json file returned from mongo db as follow: [ { "_id": { "$date": "2014-10-19T04:00:00.000Z" }, "value": 29 }, { "_id": { "$date": "2014-10-20T04:00:00.000Z" }, "value": 20 }, { "_id": { "$date": "2014-10-21T04:00:00.000Z" }, "value": 21 } ] Now I want to read the date in java in the following format: 2014/10/25 but when I use: System.out.println("DAte is : "+result.get("_id").toString() ); the result is : DAte is : Sun Oct 19 01:00:00 ADT 2014 Then only thing that comes to my mind

Is it possible to query for a range of hashes of a hashed indexed key in mongodb?

╄→гoц情女王★ 提交于 2019-12-12 02:48:20
问题 I have a hashed based sharding enabled for a key "userId" in a mongodb collection. Is it possible to get all documents whose hashed values of "userId" are between, let's say, "abcd" and "cdef"? Thanks! 回答1: One way that I found is to use the following query: db.mycollection.find({ "$and": [ {_id: {"$gt": -5012941854059245285}}, {_id : {"$lt": -4712634714892678072}} ]}).hint({_id: "hashed"}) where the long numbers are hashed values of _id. It works for me, but is it the best way of doing so?

Near operator for geojson point returning error when maxdistance is used in query

最后都变了- 提交于 2019-12-12 02:43:20
问题 I am trying to store some geojson points in mongo db.Here is the mongoose schema I have defined. new mongoose.Schema({ itemName:String, loc:{ type: [Number], index: '2dsphere'} }); Next I am trying to run a near operator to find out some points nearby.This is how I have defined the near query. Model.where('loc').near({ center: { type:"Point",coordinates: [10,10] }, maxDistance: 20 }).exec(function(err,data){ console.log(err); if (!err && data) { console.log("Success"); } else { console.log(

MongoDB Aggregation command to Java code

让人想犯罪 __ 提交于 2019-12-12 02:42:36
问题 This is my mongodb shell command: db.dais_employee.aggregate([ { "$redact": { "$cond": { "if": { "$gt": [ { "$subtract": [ "$modifiedon", "$createdon" ] }, 1000 * 60 * 60 * 24 ] }, "then": "$$KEEP", "else": "$$PRUNE" } } } ]) which works fine. I need to work this MongoDB command in java and I need help for using MongoDB Aggregation framework with java driver. When I am trying to run this command using java driver: AggregateIterable<Document> iterable = collection.aggregate(asList( new

How to use aggregation for MongoDB to filter based on $and/$or?

99封情书 提交于 2019-12-12 02:39:57
问题 I don't quite understand to use $or or $and operators with MongoDB. Let's say I have following data: { "_id" : "0211dd50-6283-11e6-be4b-01259186ebdc", "customer_id" : 75832, "prod_display_id" : "8c890b9c-7824-11e5-9cf1-24519096eb0c", "record_ids" : [ ObjectId("1377ki5f8782b634fbf92b07b"), ObjectId("6283gf5f922b634fbf32b07a"), ObjectId("9913zd5f234b634fbf22b07c") ], "create_dt" : ISODate("2014-01-12T13:31:42.740Z") }, { "_id" : "3454ke24-9372-11e6-be4b-01259186ebdc", "customer_id" : 75832,

best practice for nested category in Mongo and Meteor [closed]

守給你的承諾、 提交于 2019-12-12 02:15:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I want to handle nested category in Mongo and Meteor framework for several Ads. For example like this: Ads object have category field like this: MainCategory_1 > SubCategory_1.1 > SubCategory_1.1.1 > SubCategory_1.1.1.1 > { Ad_1 HERE } > SubCategory_1.2 > SubCategory_1.2.1 >

Map document to object

孤者浪人 提交于 2019-12-12 02:12:17
问题 public void SearchForDate(String date) { Information object = new Information(); List<Information> list = new ArrayList<Information>(); MongoCursor<Document> cursor = collection.find(eq("date", date)).iterator(); while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } Hi, I want inserting the cursor.next().toJson() information on a new object of the information class, how I could do it? the cursor just return a String... public void BuscarPorPalabra(String date) {

Translation of sql query to Mongo languague

孤者浪人 提交于 2019-12-12 01:53:16
问题 I am trying to do the following query in Mongo: SELECT * FROM events WHERE v1 - v2 > 20; where v1 and v2 are names of columns in the SQL DB. how can I translate it to Mongo Languague? 回答1: Assume you have these documents in your events : { "_id" : ObjectId("54cdfde528bb923955eff8b4"), "v1" : 35, "v2" : 10 } /* 1 */ { "_id" : ObjectId("54cdfde928bb923955eff8b5"), "v1" : 10, "v2" : 20 } You cant use a simple find() query. You should use aggregation: db.events.aggregate( [ { $project : { 'diff'

How to access embeded documents using aggregation function [duplicate]

自古美人都是妖i 提交于 2019-12-12 01:24:49
问题 This question already has answers here : Sum in nested document MongoDB (2 answers) Closed 4 years ago . db.logdata.findOne() { "_id" : ObjectId("552cfc949258ff1fa8686f1a"), "ldl_date" : "2015-04-09", "ldl_mmo_id" : 5, "ldl_master_info_id" : 11, "ldl_publication_info_id" : 41616, "detail" : [ { "ldl_id" : 54261629, "ldl_xml_info_id" : 37437691, "ldl_distribution_id" : 3289, "ldl_local_flag" : 1, "ldl_ftp_flag" : 0, "ldl_time" : "2015-04-09 06:36:46" } ] } I need to access the ldl_local_flag

MongoDB query to remove duplicate documents from a collection

给你一囗甜甜゛ 提交于 2019-12-12 01:22:55
问题 I take data from a search box and then insert into MongoDB as a document using the regular insert query. The data is stored in a collection for the word "cancer" in the following format with unique "_id". { "_id": { "$oid": "553862fa49aa20a608ee2b7b" }, "0": "c", "1": "a", "2": "n", "3": "c", "4": "e", "5": "r" } Each document has a single word stored in the same format as above. I have many documents as such. Now, I want to remove the duplicate documents from the collection. I am unable to