mongodb-query

Passing MongoDB Block from a servlet to a JSP

雨燕双飞 提交于 2020-02-06 10:21:05
问题 I have the following Block which filters the data according to the filters I have applied; i.e. it should print only those data that match with "name" Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(eq("name", name)).forEach(printBlock); I wish to send this data from the current servlet to JSP so that I can display the data. How should I go about this? EDIT: JSP Page: <form

How to get embedded document in mongodb? [duplicate]

我与影子孤独终老i 提交于 2020-02-06 08:01:31
问题 This question already has answers here : Retrieve only the queried element in an object array in MongoDB collection (11 answers) Closed 3 months ago . I tried to get embedded documents in the table using mongodb but its not working. dev table [{ "id":1, "data":[{"id":1,"name":true},{"id":2,"name":true},{"id":3,"name":false},{"id":1,"name":true}] }] Query db.dev,find({data.name:true}) Excepted output [{ "id":1, "data":[{"id":1,"name":true},{"id":2,"name":true}] }] I got Output [{ "id":1, "data

How to use findOneAndUpdate using an aggregated query in mongoDB?

依然范特西╮ 提交于 2020-02-06 08:01:08
问题 I am using nestJS as a backend connected with MongoDB. I am able to access a nested array in my schema which is: { "id": "productId", "name": "productName", "price": "productPrice", "Categories": [ { "_id": "catId", "name": "catName", "Subcategories": [ { "_id": "subcatId", "name": "subcatName" }, { "_id": "subcatId", "name": "subcatName" }, ] }, { "_id": "catId", "name": "catName", "Subcategories": [ { "_id": "subcatId", "name": "subcatName" }, { "_id": "subcatId", "name": "subcatName" }, ]

How to get parent object based upon key from child map in MongoRepository JAVA SpringBoot

我怕爱的太早我们不能终老 提交于 2020-02-06 07:54:28
问题 This is how my object looks. @Document("parent") public class Parent { @Id private String id; private String field-1; private Map<String,Long> someMap; } { "_id" : "parent-id", "field-1" : "some value", "someMap" : { "category-5" : 123, "category-4" : 456 } } I want to get this entire object when i search with "category-5" or "category-4" . I am using MongoRepository and SpringBoot . I tried following approach but its not working for me public interface ParentRepo extends MongoRepository

Apply uppercase in all the fields of all documents (MongoDB - aggregate)

假装没事ソ 提交于 2020-02-05 04:25:07
问题 Suppose that I have documents like these: [ { "one": "aaa", "two": "bbb", "three": "aba" }, { "one": "dd", "two": "cc", } ] Is there a way (with aggregate) to apply a function to every field? Like this? db.collection.aggregate([ { '$project': { 'SOME FUNCTION TO UPPERCASE ALL THE FIELDS IN A ONCE' } } ]) Expected result : [ { "one": "AAA", "two": "BBB", "three": "ABA" }, { "one": "DD", "two": "CC", } ] 回答1: Please try this : db.collection.aggregate([{ /** Adding a new field data with required

Mongodb Aggregation by 5 minutes

☆樱花仙子☆ 提交于 2020-02-04 05:37:47
问题 I have IoT sensors which keep sending events every second, and storing the events in MongoDB below format: { "_id" : "stp_23", "HP" : [ 1261.0, 1357.0, 1337.0, 1250.0, 1269.0, 1134.0, 1219.0, 1269.0, 1166.0, 1361.0, 1246.0, ... ] "TS" : [ "2019-12-20T00:00:04.6440124Z", "2019-12-20T00:00:14.6440124Z", "2019-12-20T00:00:24.6440124Z", "2019-12-20T00:00:34.6450042Z", "2019-12-20T00:00:44.6450042Z", "2019-12-20T00:00:54.6450042Z", "2019-12-20T00:01:55.6460113Z", "2019-12-20T00:02:05.6460113Z",

$regex within $project mongodb aggregation

微笑、不失礼 提交于 2020-02-03 13:19:20
问题 I have a users collection which has a location field with type String . I want to pass a score field along the document which shows whether the document's location is similar to the text "Austin". For instance the recorded location is Austin, Texas , I want it to be matched with Austin . I thought it would be possible to use $regex for this. I wrote this aggregation: $project: { score: { $cond: if: { $regex: {'$location': /Austin/} }, then: 1, else: 0 } }, location: 1, firstName: 1, lastName:

$regex within $project mongodb aggregation

泪湿孤枕 提交于 2020-02-03 13:17:51
问题 I have a users collection which has a location field with type String . I want to pass a score field along the document which shows whether the document's location is similar to the text "Austin". For instance the recorded location is Austin, Texas , I want it to be matched with Austin . I thought it would be possible to use $regex for this. I wrote this aggregation: $project: { score: { $cond: if: { $regex: {'$location': /Austin/} }, then: 1, else: 0 } }, location: 1, firstName: 1, lastName:

MongoDB Calculate Values from Two Arrays, Sort and Limit

一笑奈何 提交于 2020-02-03 12:14:13
问题 I have a MongoDB database storing float arrays. Assume a collection of documents in the following format: { "id" : 0, "vals" : [ 0.8, 0.2, 0.5 ] } Having a query array, e.g., with values [ 0.1, 0.3, 0.4 ] , I would like to compute for all elements in the collection a distance (e.g., sum of differences; for the given document and query it would be computed by abs(0.8 - 0.1) + abs(0.2 - 0.3) + abs(0.5 - 0.4) = 0.9 ). I tried to use the aggregation function of MongoDB to achieve this, but I can

Mongoose: how to use index in aggregate?

我们两清 提交于 2020-02-02 16:14:45
问题 How can I use indexes in aggregate? I saw the document https://docs.mongodb.com/manual/core/aggregation-pipeline/#pipeline-operators-and-indexes The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. Is there any way of using index not the beginning situation? like $sort , $match or $group Please help me 回答1: An index works by keeping a record of certain pieces of data that point to a given record in your collection. Think of