Java&Mongo: get object where field exist
up vote
0
down vote
favorite
Situation:
I have collection with documents, each document contains such fields: "_id"(ObjectId), "userId"(String), but it also can be field "files"(Object).
When I'm doing this:
final Query query = new Query();
query.fields().include("_id");
final List<File> fileList = mongo.find(query, File.class);
As a result I'm getting fileList
with all records and that is fine.
But when I'm doing like that, to get records only where "files" field exists - result is error.
final Query query = new Query();
query.fields().include("files");
final List<File> fileList = mongo.find(query, File.class);
Does error occur because I'm trying to get Object field? And how I can solve this with Query or Criteria?
java spring mongodb
add a comment |
up vote
0
down vote
favorite
Situation:
I have collection with documents, each document contains such fields: "_id"(ObjectId), "userId"(String), but it also can be field "files"(Object).
When I'm doing this:
final Query query = new Query();
query.fields().include("_id");
final List<File> fileList = mongo.find(query, File.class);
As a result I'm getting fileList
with all records and that is fine.
But when I'm doing like that, to get records only where "files" field exists - result is error.
final Query query = new Query();
query.fields().include("files");
final List<File> fileList = mongo.find(query, File.class);
Does error occur because I'm trying to get Object field? And how I can solve this with Query or Criteria?
java spring mongodb
What is your intention on 2nd query? Do you want retrieve all documents that containsfiles
field?
– Miguel Cartagena
Aug 6 '13 at 15:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Situation:
I have collection with documents, each document contains such fields: "_id"(ObjectId), "userId"(String), but it also can be field "files"(Object).
When I'm doing this:
final Query query = new Query();
query.fields().include("_id");
final List<File> fileList = mongo.find(query, File.class);
As a result I'm getting fileList
with all records and that is fine.
But when I'm doing like that, to get records only where "files" field exists - result is error.
final Query query = new Query();
query.fields().include("files");
final List<File> fileList = mongo.find(query, File.class);
Does error occur because I'm trying to get Object field? And how I can solve this with Query or Criteria?
java spring mongodb
Situation:
I have collection with documents, each document contains such fields: "_id"(ObjectId), "userId"(String), but it also can be field "files"(Object).
When I'm doing this:
final Query query = new Query();
query.fields().include("_id");
final List<File> fileList = mongo.find(query, File.class);
As a result I'm getting fileList
with all records and that is fine.
But when I'm doing like that, to get records only where "files" field exists - result is error.
final Query query = new Query();
query.fields().include("files");
final List<File> fileList = mongo.find(query, File.class);
Does error occur because I'm trying to get Object field? And how I can solve this with Query or Criteria?
java spring mongodb
java spring mongodb
asked Aug 6 '13 at 15:16
Lugaru
78421528
78421528
What is your intention on 2nd query? Do you want retrieve all documents that containsfiles
field?
– Miguel Cartagena
Aug 6 '13 at 15:35
add a comment |
What is your intention on 2nd query? Do you want retrieve all documents that containsfiles
field?
– Miguel Cartagena
Aug 6 '13 at 15:35
What is your intention on 2nd query? Do you want retrieve all documents that contains
files
field?– Miguel Cartagena
Aug 6 '13 at 15:35
What is your intention on 2nd query? Do you want retrieve all documents that contains
files
field?– Miguel Cartagena
Aug 6 '13 at 15:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can construct this Query
to get all documents that contains files
filed:
Query.query(Criteria.where("files").exists(true))
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can construct this Query
to get all documents that contains files
filed:
Query.query(Criteria.where("files").exists(true))
add a comment |
up vote
2
down vote
accepted
You can construct this Query
to get all documents that contains files
filed:
Query.query(Criteria.where("files").exists(true))
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can construct this Query
to get all documents that contains files
filed:
Query.query(Criteria.where("files").exists(true))
You can construct this Query
to get all documents that contains files
filed:
Query.query(Criteria.where("files").exists(true))
answered Aug 6 '13 at 15:41
seralex.vi
55026
55026
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18084144%2fjavamongo-get-object-where-field-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is your intention on 2nd query? Do you want retrieve all documents that contains
files
field?– Miguel Cartagena
Aug 6 '13 at 15:35