mongodb-query

Cursor.hasNext throws java.util.NoSuchElementException

大憨熊 提交于 2020-01-22 02:25:28
问题 public String ForDate(String date) { MongoCursor<Document> cursor = collection.find(eq("date", date)).iterator(); basicb b = new basicb(); while (cursor.hasNext()) { b.setDepartament(cursor.next().getString("departament")); b.setText(cursor.next().getString("text")); b.setTitle(cursor.next().getString("title")); lista.add(b); } } I just want to do object from mongodb information but when I do this method get make some object but always return that errorjava.util.NoSuchElementException. 回答1:

update multi records and insert if not present

拟墨画扇 提交于 2020-01-21 23:16:27
问题 Lets say my test data is db.multiArr.insert({"ID" : "fruit1","Keys" : "apple"}) db.multiArr.insert({"ID" : "fruit2","Keys" : "carrot"}) db.multiArr.find({'ID': {$in: ['fruit1', 'fruit2']}}) if i want to update or insert a ID i can do it using db.multiArr.update( {'ID': "fruit12"}, { 'ID': "fruit12" "$push": { "Keys": "tomato" } }, upsert=True ) i want to update or insert multiple records, i know the below query inserts only 1 row db.multiArr.update( {"ID": {"$in: ["fruit123", "fruit1234"]}},

update multi records and insert if not present

Deadly 提交于 2020-01-21 23:15:59
问题 Lets say my test data is db.multiArr.insert({"ID" : "fruit1","Keys" : "apple"}) db.multiArr.insert({"ID" : "fruit2","Keys" : "carrot"}) db.multiArr.find({'ID': {$in: ['fruit1', 'fruit2']}}) if i want to update or insert a ID i can do it using db.multiArr.update( {'ID': "fruit12"}, { 'ID': "fruit12" "$push": { "Keys": "tomato" } }, upsert=True ) i want to update or insert multiple records, i know the below query inserts only 1 row db.multiArr.update( {"ID": {"$in: ["fruit123", "fruit1234"]}},

MongoDB Aggregate & Grouping Issue in MeteorJS

泪湿孤枕 提交于 2020-01-21 19:21:31
问题 Using MongoDB in Meteor JS, how do you use Meteor Aggregate properly? The intended result is to return grouped users by their userId and sum up a boolean field called "progressState" (true/false). For example the document can have: user 001 - true user 001 - false user 001 - true user 003 - false user 005 - true but the intended result would be: user 001: 2 true user 003: 0 true user 005: 1 true etc.. My attempt gives the following error: "exception: FieldPath field names may not start with '

Merge $lookup result to existing array

ぐ巨炮叔叔 提交于 2020-01-21 18:04:30
问题 I'm new to mongo and I need your help. I have collection studijneProgramy . This is sample document: { "_id" : "dGFY", "garranti" : [ { "typ" : { "sk" : "garant", "en" : "Chairman of study board" }, "id" : "1025769" }, { "typ" : { "sk" : "predseda odborovej komisie", "en" : "Chairman of study board" }, "id" : "1025769" } ] } Next I have collection osoby . Example document: { "_id" : "1025769", "plneMeno" : "prof. RNDr. Peter Moczo, DrSc.", "priezvisko" : "Moczo", "meno" : "Peter",

mongo group and count with condition

China☆狼群 提交于 2020-01-21 04:18:26
问题 I'm trying to group a set of documents and count them based on their value. For example { "_id" : 1, "item" : "abc1", "value" : "1" } { "_id" : 2, "item" : "abc1", "value" : "1" } { "_id" : 3, "item" : "abc1", "value" : "11" } { "_id" : 4, "item" : "abc1", "value" : "12" } { "_id" : 5, "item" : "xyz1", "value" : "2" } Here I would like to group by "item" and get in return a count how many times the "value" is bigger than 10 and how many times smaller. So: { "item": "abc1", "countSmaller": 2,

Mongodb aggregate sort and limit within group [duplicate]

风流意气都作罢 提交于 2020-01-20 05:17:04
问题 This question already has answers here : mongodb group values by multiple fields (3 answers) Closed 11 months ago . I have a collection of items for sale with the following schema: var itemSchema = new Schema({ "category" : { type : Schema.Types.ObjectId, ref : 'Category' }, "merchant" : { type : Schema.Types.ObjectId, ref : 'Merchant' }, "rating" : Number }) I have inherited an aggregate query which returns items matching a category, grouped by merchant, with the groups sorted by the maximum

How to join multiple collections with $lookup in mongodb

那年仲夏 提交于 2020-01-18 04:10:08
问题 I want to join more than two collections in MongoDB using the aggregate $lookup. Is it possible to join? Give me some examples. Here I have three collections: "users" { "_id" : ObjectId("5684f3c454b1fd6926c324fd"), "email" : "admin@gmail.com", "userId" : "AD", "userName" : "admin" } "userinfo" { "_id" : ObjectId("56d82612b63f1c31cf906003"), "userId" : "AD", "phone" : "0000000000" } "userrole" { "_id" : ObjectId("56d82612b63f1c31cf906003"), "userId" : "AD", "role" : "admin" } 回答1: The join

Can Mongo use a reference and compare values during aggregation?

天大地大妈咪最大 提交于 2020-01-16 22:26:14
问题 Given this document: { "_id" : ObjectId("53f7287881a97c71e58e3514"), "title" : "My Page", "currentVersion" : 1, "versions" : [ { "version" : 0, "content" : [ { "data" : "foo", } ] }, { "version" : 1, "content" : [ { "data" : "bar", } ] } ] } Is there anyway, (via the aggregation framework or otherwise) to perform a document projection where the only content in versions is the one that satisfies versions.version==currentVersion for each document? I understand that the first step might be to

Can Mongo use a reference and compare values during aggregation?

孤街醉人 提交于 2020-01-16 22:25:27
问题 Given this document: { "_id" : ObjectId("53f7287881a97c71e58e3514"), "title" : "My Page", "currentVersion" : 1, "versions" : [ { "version" : 0, "content" : [ { "data" : "foo", } ] }, { "version" : 1, "content" : [ { "data" : "bar", } ] } ] } Is there anyway, (via the aggregation framework or otherwise) to perform a document projection where the only content in versions is the one that satisfies versions.version==currentVersion for each document? I understand that the first step might be to