In a project I'm working on, at one point I read a query to mongodb from a string. I've been using com.mongodb.util.JSON.parse(querystring) to read the query, which worked fine until I started reading queries that contained operators like $max and $min. At that point, rather than using mongodb's $max operator, the parser instead creates a "$max" field. For instance,
the input string:
{ $query : { state : "AL" } , $max : { pop : 9058 } }
is parsed to the DBObject:
{ "$query" : { "state" : "AL"} , "$max" : { "pop" : 9058}}
When I then look for a DBCursor with that query document, I get a cursor of size 0 (no matching document found in the databse), presumably because there are no documents with "$query" or "$max" fields.
Is there something I can use besides JSON.parse()? I'm not averse to writing my own function for it, but how can I get a DBObject that recognizes the $ operators as operators and not fields?
Any advice would be appreciated!
The following code snippet using query modification operator $max seems to work fine.
/* {$query:{state:"AL"}, "$max":{pop:10000}}*/
String s = "{$query:{state:\"AL\"}, \"$max\":{pop:10000}}";
DBObject dbObject = (DBObject) JSON.parse(s);
System.out.println("\nFind all: ");
DBCursor cursor = collection.find(dbObject);
try {
while (cursor.hasNext()) {
DBObject cur = cursor.next();
System.out.println(cur);
}
} finally {
cursor.close();
}
Make sure you have specified index on pop.
db.zips.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.zips",
"name" : "id"
},
{
"v" : 1,
"key" : {
"pop" : 1
},
"ns" : "test.zips",
"name" : "pop_1"
}
]
See the following link for detail.
http://docs.mongodb.org/manual/reference/operator/max/
Just in case you are interested in using aggregation operators $max or $min, the following link provide details and sample code.
http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/
So it turns out the DBObject as given up there worked out fine. It returns a cursor with a size of 0, true, but the DBCursor's length is actually the thing I was looking for. (Previously, I had been checking whether the cursor's size was 0, and if it was, returning null.)
I'm not quite sure what the difference between size and length is in a DBCursor (the difference between size and count is apparent, but I'm not sure what length is supposed to be), but it works now. In the case above, size and count were both 0 but length was the desired number.
Related
I have the following MongoDB document structure that I am trying to query;
{
"key": [{
"1": [
2,
3,
4
]
},
{
"2": [
1
]
}
]
}
What I want is all documents having inside the key field having their sub field as "1". The array associated with that is [2,3,4] which are java Long values. I am trying to do the above logic with the following code with no luck;
BasicDBObject query = new BasicDBObject("key.1", null);
MongoCursor<BasicDBObject> cursor = collection.find(query).iterator();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
The reason I am associating key.1 with null in the query object is because I don't care what the values in the array is.
You're mixing couple of things here.
key.1 is conflicting dot notation syntax for querying the sub fields with numeric string name.
This will conflict with indexed based access for the array key.
BasicDBObject query = new BasicDBObject("key.1", null); in effect is asking Mongo to query for a key array for a value / document at index 1 for a null value . This would have matched if you had something like
{
"key":
[
{"1":"one"},
null
]
}
Okay now coming back to post. You've to use $exist operator if you don't care about value. The shell filter will be {"key":{"$elemMatch":{"1":{ $exists: true}}}}. Note the use of $elemMatch to do field level comparison as dot notation is conflicting with index style access. For string name fields the dot notation & elemMatch works similarly for embedded arrays for queries involving single query condition.
Java code
BasicDBObject exists = new BasicDBObject("$exists", true);
BasicDBObject field = new BasicDBObject("1", exists);
BasicDBObject elemMatch = new BasicDBObject("$elemMatch", field);
BasicDBObject query = new BasicDBObject("key", elemMatch);
More info about operators and syntax.
https://docs.mongodb.com/manual/reference/operator/query/exists/
https://docs.mongodb.com/manual/reference/operator/query/elemMatch/#op._S_elemMatch
https://docs.mongodb.com/manual/core/document/
I have few json document with the following format :-
_source: {
userId: "A1A1",
customerId: "C1",
component: "comp_1",
timestamp: 1408986553,
}
I want to query the document based on the following :-
(( userId == currentUserId) OR ( customerId== currentCustomerId) OR (currentRole ==ADMIN) ) AND component= currentComponent)
I tried using the SearchSourceBuilder and QueryBuilders.matchQuery, but I wasnt able to put multiple sub queries with AND and OR operators.
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("userId",userId)).sort("timestamp", SortOrder.DESC).size(count);
How we query elasticsearch using OR and AND operators?
I think in this case the Bool query is the best shot.
Something like :
{
"bool" : {
"must" : { "term" : { "component" : "comp_1" } },
"should" : [
{ "term" : { "userId" : "A1A1" } },
{ "term" : { "customerId" : "C1" } },
{ "term" : { "currentRole" : "ADMIN" } }
],
"minimum_should_match" : 1
}
}
Which gives in Java:
QueryBuilder qb = QueryBuilders
.boolQuery()
.must(termQuery("component", currentComponent))
.should(termQuery("userId", currentUserId))
.should(termQuery("customerId", currentCustomerId))
.should(termQuery("currentRole", ADMIN))
.minimumNumberShouldMatch(1)
The must parts are ANDs, the should parts are more or less ORs, except that you can specify a minimum number of shoulds to match (using minimum_should_match), this minimum being 1 by default I think (but you could set it to 0, meaning that a document matching no should condition would be returned as well).
If you want to do more complex queries involving nested ANDs and ORs, simply nest other bool queries inside must or should parts.
Also, as you're looking for exact values (ids and so on), maybe you can use term queries instead of match queries, which spare you the analysis phase (if those fields are analyzed at all, which doesn't necessarily make sense for ids). If they are analyzed, you still can do that, but only if you know exactly how your terms are stored (standard analyzer stores them lower cased for instance).
If you use a query_string query, your ANDs and ORs will be interpreted as such by the Lucene library.
This allows you to search for
(currentUserId OR currentCustomerId) AND currentComponent
for instance. By default, the values will be searched for in all fields.
I have a collection in mongoDB and its documents have 3 fields like below:
{
"_id" : { "$oid" : "5396ad5de4b09ea27a641ed6"} ,
"word" : "test_word" ,
"doc_occurrence" : "'total':25,'sport':10" ,
"total_occurrence" : "'total':32,'sport':15"
}
I want to know can I update value of the key directly using java (using dot for example)? or I have to get map completely and update this way?
If i have to use sub-document to update values through dot notation,what type of data type I must use to keep sub-doument fields?
p.s: above collection structure is what is printed in java.
collection structure in mongo terminal is like this:
{
"_id" : ObjectId("5396d751e4b0cabeab49be0b"),
"word" : "test_word",
"doc_occurrence" : { "total" : 25, "sport" : 10 },
"total_occurrence" : { "total" : 32, "sport" : 15 }
}
These you can update a single field by the following:
BasicDBObject query = new BasicDBObject(
"_id", new ObjectId("5396ad5de4b09ea27a641ed6")
);
BasicDBObject update = new BasicDBObject(
"$set", new BasicDBObject("doc_occurance.total", 30)
);
table.update(query,update);
So this uses the $set operator and the use of "dot notation" to indentify and singularly update an element in a sub-document. You can specify as many fields as you need within the $set operator part of the update.
I assume you're looking for findAndModify. This question more or less treats your problem: MongoDb's $set equivalent in its java Driver.
I am also not sure about the structure of your document ... maybe you forgot a few { } at the doc_occurrence and total_occurrence.
I need some help updating property of embedded collection with JSON structure below -
translation
{
"_id" : ObjectId("533d4c73d86b8977fda970a9"),
"_class" : "com.xxx.xxx.translation.domain.Translation",
"locales" : [
{
"_id" : "en-US",
"description" : "English (United States)",
"isActive" : true
},
{
"_id" : "pt-BR",
"description" : "Portuguese (Brazil)",
"isActive" : true
},
{
"_id" : "nl-NL",
"description" : "Dutch (Netherlands)",
"isActive" : true
}
],
"screens" : [
{
"_id" : "caseCodes",
"dictionary" : [
{
"key" : "CS_CAT1",
"parameterizedValue" : "My investigations",
"locale" : "en-US"
},
{
"key" : "MY_INVESTIGATIONS",
"parameterizedValue" : "",
"locale" : "pt-BR"
},
}
]
}
In above structure:
I want to update "parameterizedValue" usinng spring-data-mongo-db API 1.3.4, for screen with _id="caseCodes" and key = "CS_CAT1".
I tried (here 'values' is an collection name for TranslationValue array)
mongoOperations.updateFirst(Query.query(Criteria.where("screens._id")
.is("caseCodes")), new Update().push(
"screens.dictionary.$.values", translationValue),
Translation.class);
but it said, "can't append array to string "dictionary"....
Any pointers or help here? Thanks.
-Sanjeev
There are a few problems with your logic as well as problems with your schema for this type of update.
Firstly what you have are nested arrays, and this causes a problem with updates as described in the documentation for the positional $ operator. What this means is that any condition matching an element of an array on the query side of the update statement will only match the first array index found.
Since you need a specific entry in the inner array you would need to match that as well. But the "catch" says that only the first match will be used in the positional operator so you cannot have both. The query form (if it were possible to work, which it does not) would actually be something like this (native shell):
db.collection.update(
{
"screens._id": "caseCodes",
"screens.dictionary.key": "CS_CAT1"
},
{
"$set": {
"screens.$.dictionary.$.parameterizedValue": "new value"
}
}
)
Now that would "appear" to be more correct than what you are doing, but of course this fails because the positional operator cannot be used more than once. I may just quite stupidly work in this case as it just so happens that the first matched index of the "screens" array (which is 0) happens to be exactly the same as the required index of the inner element. But really that is just "dumb luck".
To illustrate better, what you need to do with these type of updates is already know the indexes of the elements and place those values directly into the update statement using "dot notation". So updating your second "dictionary" element would go like this:
db.collection.update(
{
"screens._id": "caseCodes",
"screens.dictionary.key": "MY_INVESTIGATIONS"
},
{
"$set": {
"screens.0.dictionary.1.parameterizedValue": "new value"
}
}
)
Also noting that the correct operator to use here is $set as you are not appending to either of the arrays, but rather you wish to change an element.
Since that sort of exactness in updates is unlikely to suit what you need, then you should look at changing the schema to accommodate your operations in a much more supported way. So one possibility is that your "screens" data may not possibly need to be an array, and you could change that to a document form like so:
"screens" : {
"caseCodes": [
{
"key" : "CS_CAT1",
"parameterizedValue" : "My investigations",
"locale" : "en-US"
},
{
"key" : "MY_INVESTIGATIONS",
"parameterizedValue" : "",
"locale" : "pt-BR"
},
]
}
This changed the form to:
db.collection.update(
{
"screens.caseCodes.key": "CS_CAT1"
},
{
"$set": {
"screens.caseCodes.$.parameterizedValue": "new value"
}
}
)
That may or may not work for your purposes, but you either live with the limitations of using a nested array or otherwise change your schema in some way.
I want to create compound index on Age and Name in MongoDB through Java driver and here is my syntax:
coll.ensureIndex(new BasicDBObject("Age", 1),new BasicDBObject("Name", -1));
List <DBObject> list = coll.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
but it create only 1 index not compund index and give me result this:
{ "v" : 1 , "key" : { "_id" : 1} ,"ns" :"EmployeeData.EmpPersonalData", "name":"_id_"}
{ "v" : 1 , "key" : { "Age" : 1} , "ns" : "EmployeeData.EmpPersonalData" , "name" : "Age_1" , "Name" : -1}
So how can compund index on collection can be created through java driver?
If you look at your code, you have actually called ensureIndex with two parameters. Your first parameter was the key and your second parameter became some extra field: Name: -1.
What you are looking to pass in the first parameter is this object {"Age":1, "Name":-1}. What you actually passed was {"Age":1}, {"Name":-1}.
So you want to do something like this:
BasicDBObject obj = new BasicDBObject();
obj.put("Age", 1);
obj.put("Name", -1);
coll.ensureIndex(obj);
Note that the index will be created with a default name. To provide a specific name do the following:
coll.ensureIndex(obj, "MyName");
You can try this according to the official documentation.
import org.bson.Document;
db.getCollection("restaurants").createIndex(new Document("cuisine", 1).append("address.zipcode", -1));
Official Mongo DB Driver Java documentation
A more modern and perhaps fluent approach could be:
import com.mongodb.client.model.Indexes;
collection
.createIndex(
Indexes.compoundIndex(
Indexes.ascending("Age"),
Indexes.descending("Name")
)
);
Found in the manual. Formatted for clarity; post-format to your own taste.