Discussion:
[mongodb-user] Query by Specific Fields & Group by Data
KARTHIK N.S
2017-06-30 10:14:20 UTC
Permalink
Hi

Spec : Mongodb : 3.0.12 , mongo-java-driver-3.3.0.jar,

Using a Mongo Shell, the following retrieves the specific fields


*db.TABLE_TEST.find({"cola":"1000"},{"colb":1,"colc":1,"cold":1}});*

Question : a) Using Java Mongo API 3.x How to fetch the same ?

b) Also if the the 'col' data is repetitive how to
fetch unique full dataset based on the specific col ?


with regards
Karthik
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/92dd94f8-6c7d-4d5d-8da9-4be09190bd63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Wan Bachtiar' via mongodb-user
2017-07-04 04:03:50 UTC
Permalink
a) Using Java Mongo API 3.x How to fetch the same ?

Hi Karthik,

For MongoDB Java Driver v3.3, please see Getting Started: query the
collection
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#query-the-collection>
and Getting Started: Projecting fields
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#projecting-fields>

If you still have further questions please provide the Java code that
you’ve tried and if any, error messages that you’re getting.

b) Also if the the ‘col’ data is repetitive how to fetch unique full
dataset based on the specific col ?

So the col data is not unique, but you’re wanting to fetch specific col ?
I’m not sure I understand what you’re trying to query.

Could you please provide example document(s) and what you’re trying to
retrieve to elaborate your question better ?

You may find MongoDB Java Driver v3.3 Getting Started: aggregations
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#aggregations>
, Aggregation pipeline <https://docs.mongodb.com/manual/aggregation/> and $group
aggregation stage
<https://docs.mongodb.com/manual/reference/operator/aggregation/group/>
useful if you’re attempting to group dataset by a certain value.

Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/b42d18a4-d1a2-4e00-833f-86c0012a551e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
KARTHIK N.S
2017-07-04 08:13:46 UTC
Permalink
Hi Wan Thx for the reply

Let me brief u on the equation

TABLE_TEST Json Data in the Collection as i have is as 4 key/values per
document.

{ "_id" : { "$oid" : "59520ba4a33f9c173c316482" }, "cola" : "1000",
"colb" : "2001", "colc" : "3000","cold":"4000"}
{ "_id" : { "$oid" : "59520ba4abcd9c173c316482" }, "cola" : "1000",
"colb" : "2002", "colc" : "3000","cold":"4000"}
{ "_id" : { "$oid" : "59520ba4abcd9c173c316482" }, "cola" : "1000",
"colb" : "2003", "colc" : "3000","cold":"4000"}

My requirement works in MongoShell
*db.TABLE_TEST.find({"cola":"1000"},{"colb":1,"colc":1});*

O/p is as

{"_id" : ObjectId("595b437effe36920d0a4663a"), "colb" : "2001", "colc" :
"3000"}
{"_id" : ObjectId("595b43a7ffe36920d0a4663c"), "colb" : "2002", "colc" :
"3000"}
{"_id" : ObjectId("595b43b9ffe36920d0a4663d"), "colb" : "2003", "colc" :
"3000"}

So : I need Java 3.x equivalent for
*db.TABLE_TEST.find({"cola":"1000"},{"colb":1,"colc":1});*

If u have a reference / sample code, plz share.

with regards
Karthik
Post by KARTHIK N.S
a) Using Java Mongo API 3.x How to fetch the same ?
Hi Karthik,
For MongoDB Java Driver v3.3, please see Getting Started: query the
collection
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#query-the-collection>
and Getting Started: Projecting fields
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#projecting-fields>
If you still have further questions please provide the Java code that
you’ve tried and if any, error messages that you’re getting.
b) Also if the the ‘col’ data is repetitive how to fetch unique full
dataset based on the specific col ?
So the col data is not unique, but you’re wanting to fetch specific col ?
I’m not sure I understand what you’re trying to query.
Could you please provide example document(s) and what you’re trying to
retrieve to elaborate your question better ?
You may find MongoDB Java Driver v3.3 Getting Started: aggregations
<http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#aggregations>
, Aggregation pipeline <https://docs.mongodb.com/manual/aggregation/> and $group
aggregation stage
<https://docs.mongodb.com/manual/reference/operator/aggregation/group/>
useful if you’re attempting to group dataset by a certain value.
Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/a0a40e39-e3bc-4134-aca2-72eea15609c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Wan Bachtiar' via mongodb-user
2017-07-05 00:00:00 UTC
Permalink
I need Java 3.x equivalent for
*db.TABLE_TEST.find({“cola”:”1000”},{“colb”:1,”colc”:1});*

Hi Karthik,

See below for the equivalent of the above:

MongoDatabase database = mongoClient.getDatabase("databaseName");
MongoCollection<Document> collection = database.getCollection("collectionName");
collection.find(eq("cola", "1000"))
.projection(new Document("colb", 1)
.append("colc", 1))
.forEach(printBlock);

See also prerequisites
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/#prerequisites>
for the required import statements and the printBlock function.

I would recommend you to review MongoDB Java Driver Tutorials
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/> to cover
the basic operations. The tutorials also provide some examples for you to
use.

Below resources should also help you to be familiar with MongoDB Java
driver:

- MongoDB Java Driver Quick Start
<http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/>
- Github example Java code: QuickTour.java file
<https://github.com/mongodb/mongo-java-driver/blob/master/driver/src/examples/tour/QuickTour.java>

In addition, I would encourage you to enrol at MongoDB University for a
free online course on M101J: MongoDB for Java Developers
<https://university.mongodb.com/courses/M101J/about>. The course will go
over basic installation, JSON, schema design, querying, insertion of data,
indexing and working with the Java driver.

Regards,

Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/d299486d-d564-4088-b8da-945252753e0b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
N.S.KARTHIK
2017-07-07 16:27:05 UTC
Permalink
Hey Wan

Thx for the Code sample.

How ever I could not find *Java 3.x API support for 'eq' ( Package in
mongo-java-driver-3.3.0.jar)*

Is there some Alternate API to suppor the same.


with regards
Karthik
Post by 'Wan Bachtiar' via mongodb-user
I need Java 3.x equivalent for
*db.TABLE_TEST.find({“cola”:”1000”},{“colb”:1,”colc”:1});*
Hi Karthik,
MongoDatabase database = mongoClient.getDatabase("databaseName");
MongoCollection<Document> collection = database.getCollection("collectionName");
collection.find(eq("cola", "1000"))
.projection(new Document("colb", 1)
.append("colc", 1))
.forEach(printBlock);
See also prerequisites
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/#prerequisites>
for the required import statements and the printBlock function.
I would recommend you to review MongoDB Java Driver Tutorials
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/> to
cover the basic operations. The tutorials also provide some examples for
you to use.
Below resources should also help you to be familiar with MongoDB Java
- MongoDB Java Driver Quick Start
<http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/>
- Github example Java code: QuickTour.java file
<https://github.com/mongodb/mongo-java-driver/blob/master/driver/src/examples/tour/QuickTour.java>
In addition, I would encourage you to enrol at MongoDB University for a
free online course on M101J: MongoDB for Java Developers
<https://university.mongodb.com/courses/M101J/about>. The course will go
over basic installation, JSON, schema design, querying, insertion of data,
indexing and working with the Java driver.
Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5b088f16-4762-4a56-ade9-86a4c4e3e315%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
N.S.KARTHIK
2017-07-12 09:24:16 UTC
Permalink
Hi Wan



.projection(new Document("colb", 1)


How to apply this for a MongoCursor<Document> ,so that i can itrate and
fetch all of the documents with limiting to projected names only ??


with regards
Karthik
Post by 'Wan Bachtiar' via mongodb-user
I need Java 3.x equivalent for
*db.TABLE_TEST.find({“cola”:”1000”},{“colb”:1,”colc”:1});*
Hi Karthik,
MongoDatabase database = mongoClient.getDatabase("databaseName");
MongoCollection<Document> collection = database.getCollection("collectionName");
collection.find(eq("cola", "1000"))
.projection(new Document("colb", 1)
.append("colc", 1))
.forEach(printBlock);
See also prerequisites
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/#prerequisites>
for the required import statements and the printBlock function.
I would recommend you to review MongoDB Java Driver Tutorials
<http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/> to
cover the basic operations. The tutorials also provide some examples for
you to use.
Below resources should also help you to be familiar with MongoDB Java
- MongoDB Java Driver Quick Start
<http://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/>
- Github example Java code: QuickTour.java file
<https://github.com/mongodb/mongo-java-driver/blob/master/driver/src/examples/tour/QuickTour.java>
In addition, I would encourage you to enrol at MongoDB University for a
free online course on M101J: MongoDB for Java Developers
<https://university.mongodb.com/courses/M101J/about>. The course will go
over basic installation, JSON, schema design, querying, insertion of data,
indexing and working with the Java driver.
Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/30256023-d8a3-4de9-9428-f1ec400bd01b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Wan Bachtiar' via mongodb-user
2017-07-13 01:19:12 UTC
Permalink
How ever I could not find *Java 3.x API support for ‘eq’ ( Package in
mongo-java-driver-3.3.0.jar)*

Hi Karthik,

eq is part of the com.mongodb.client.model.Filters . For example:

import static com.mongodb.client.model.Filters.eq;

See also
http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/#prerequisites
.

How to apply this for a MongoCursor ,so that i can itrate and fetch all of
the documents with limiting to projected names only ??

You can try below example to use MongoCursor with projection:

MongoCursor<Document> cursor = collection.find(eq("cola", "1000")).projection(new Document("colb", 1)).iterator(); try {
while (cursor.hasNext()){
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}

See also :

http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#find-all-documents-in-a-collection

Please review the MongoDB Java driver tutorial, if you’re using v3.3 please
see:

http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/

You may also benefit from glancing over an example of Java code:

https://github.com/mongodb/mongo-java-driver/blob/3.4.x/driver/src/examples/tour/QuickTour.java

Regards,

Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/3d0bad3f-f921-4ff4-add6-6cdccf52aaf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
N.S.KARTHIK
2017-07-17 06:49:40 UTC
Permalink
Hi Wan

Thx for the reply and apologies for Late answer.

cursor = collection.find(eq("cola", "1000")).projection(new
Document("colb", 1)).iterator();

I am also awaiting for any sample extension to this code sample for
*grouping* the data for 'colb'

If have any plz share , this would help me understand the grouping
concept on specific key's with duplicate data.


with regards
Karthik
Post by 'Wan Bachtiar' via mongodb-user
How ever I could not find *Java 3.x API support for ‘eq’ ( Package in
mongo-java-driver-3.3.0.jar)*
Hi Karthik,
import static com.mongodb.client.model.Filters.eq;
See also
http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/perform-read-operations/#prerequisites
.
How to apply this for a MongoCursor ,so that i can itrate and fetch all of
the documents with limiting to projected names only ??
MongoCursor<Document> cursor = collection.find(eq("cola", "1000")).projection(new Document("colb", 1)).iterator(); try {
while (cursor.hasNext()){
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/#find-all-documents-in-a-collection
Please review the MongoDB Java driver tutorial, if you’re using v3.3
http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/
https://github.com/mongodb/mongo-java-driver/blob/3.4.x/driver/src/examples/tour/QuickTour.java
Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/8d476095-42c1-4168-ac7f-4530f6a67a9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...