I am trying to convert the mongodb shell code below to java. However, i run into some problems doing so. Can anyone help me with this issue?
MongoDB
var friend_ids = db.users.findOne(ObjectId("...")).friend_ids
db.users.find({_id:{$in:friend_ids}})
Java
ObjectId id = new ObjectId("...");
BasicDBObject fields = new BasicDBObject("friend_ids", 1).append("_id", false);
DBObject f_ids = coll.findOne(id, fields);
BasicDBObject query = new BasicDBObject("_id",(new BasicDBObject("$in", f_ids)));
DBCursor cursor = coll.find(query);
The java query is as follows.
query={ "_id" : { "$in" : { "friend_ids" : [ { "$oid" : "..."} , { "$oid" : "..."} , { "$oid" : "..."}]}}},
thanks
You need extract friend_ids from f_ids.
BasicDBObject query = new BasicDBObject("_id",(new BasicDBObject("$in", f_ids.get("friend_ids"))));
Related
I have following document :
{
"_id" : ObjectId("5d42501efbf6f6108277ceb3"),
"customer" : {
"id" : "02c59458-8f0a-4a11-bff6-55b4710bc546",
"name" : "brookfield"
},
"userId" : "86c241de-16de-417d-8537-ed441fee9b0e",
"username" : "Fonville_f888f976_un_",
"userGroups" : [],
"lastSeen" : ISODate("2020-05-06T20:21:49.140Z"),
"devices" : [
{
"id" : "A920B6F24B63479B8796B709EA542951",
"displayId" : "A920B6F24B63479B8796B709EA542951",
"manufacturedId" : false,
"lastSeen" : ISODate("2020-03-20T14:36:24.961Z"),
"product" : {
"manufacturer" : "PSman",
"manufacturerCode" : 10,
"name" : "BlackHawk 880 series",
"modelId" : "C053"
}
},
{
"id" : "2476B0E045712cc-5904-11ea-af28-0612af3fc862",
"displayId" : "2476B0E16929512870992618563",
"lastSeen" : ISODate("2020-01-09T20:59:48.650Z"),
"product" : {
"manufacturer" : "GN",
"manufacturerCode" : 5,
"name" : "Telenor Speak 710",
"modelId" : "2476",
"vendorId" : "B0E"
}
}
],
"software" : {
"applicationId" : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"version" : "3.12.52334.30629",
"displayVersion" : "3.12.0"
},
"lastHost" : {
"name" : "hn_5934d3c5dbf",
"platform" : "WIN",
"osVersion" : "10.0.17763"
}
}
I have to update every product name inside devices array based on product modelId field.
So, user will pass two modelId values and if one of them match product's modelId than product name for that product should be updated.
I wrote following code, and I tested it with mongo db version 4.2, and it works. The problem is that when i tested it with older version of mongo db (3.4.23), it doesn't work,and I'm getting following exception :
com.mongodb.MongoCommandException: Command failed with error 9 (FailedToParse): 'Unrecognized field in update operation: arrayFilters' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Unrecognized field in update operation: arrayFilters", "code" : 9, "codeName" : "FailedToParse" }
Here is how my method looks like :
private void updateCollection(String firstPid,String secondPid, String newProductName) {
MongoCollection<Document> collection = mongoTemplate.getCollection("user");
BasicDBObject searchQueryOne = new BasicDBObject();
BasicDBObject searchQueryTwo = new BasicDBObject();
BasicDBList or = new BasicDBList();
BasicDBObject query = new BasicDBObject("$or", or);
BasicDBObject updateQuery = new BasicDBObject();
searchQueryOne.append("devices.product.modelId", firstPid);
searchQueryTwo.append("devices.product.modelId", secondPid);
or.add(searchQueryOne);
or.add(searchQueryTwo);
updateQuery.append("$set",
new BasicDBObject().append("devices.$[elem].product.name", newProductName));
collection.updateMany(query, updateQuery, new UpdateOptions().arrayFilters(
Collections.singletonList( Filters.in("elem.product.modelId", Arrays.asList(firstPid,secondPid)))));
}
Please help
After some resarching, I managed to solve my problem. If someone have similar issue, here is the code that worked for me
public void updateUserCollection(String pidOne, String pidTwo) {
MongoCollection<Document> userCollection = mongoTemplate.getCollection("user");
BasicDBObject deviceCriteria = new BasicDBObject("devices.product.modelId", new BasicDBObject("$in", Arrays.asList(pidOne)));
FindIterable<Document> cursor = userCollection.find(deviceCriteria);
MongoCursor<Document> iterator = cursor.iterator();
while (iterator.hasNext()) {
Document user = iterator.next();
String userId = user.get("userId", String.class);
List<Document> devices = user.get("devices", List.class);
for (Document device : devices) {
Document product = device.get("product", Document.class);
String modelId = product.get("modelId", String.class);
if (modelId != null && (modelId.equals(pidOne) || modelId.equals(pidTwo))) {
product.put("name", newProductName);
}
}
saveUser(userId, user, userCollection);
}
iterator.close();
}
private void saveUser(String userId, Document user, MongoCollection<Document> userCollection) {
BasicDBObject updateQuery = new BasicDBObject("$set", user);
BasicDBObject searchQuery = new BasicDBObject("userId", userId);
userCollection.updateMany(searchQuery, updateQuery);
}
My data is formatted as:
{
"_id" : "149",
"books" : {
"32" : "0.12",
"33" : "0.21"
}
}
I would like to update/insert values inside the nested books document. If I insert a new row say "39" : "0.19", then the updated document should look as below:
{
"_id" : "149",
"books" : {
"32" : "0.12",
"33" : "0.21",
"39" : "0.19"
}
}
And updates should work, the way they are meant to work. By updating the values.
I tried several ways, but couldn't update the way I wanted.
Method1: Working but wrong result
MongoCollection<Document> document = mongoTemplate.getCollection("booksCollection");
BasicDBObject query = new BasicDBObject();
query.put("_id", storeId);
BasicDBObject bookDiscount = new BasicDBObject();
bookDiscount.put(bookId, discount);
BasicDBObject update = new BasicDBObject();
update.put('$push', new BasicDBObject("books", bookDiscount));
document.findOneAndUpdate(query, update);
Method1 Output: Each value is added in a new row
{
"_id" : "1664",
"books" : [
{
"28" : NumberDecimal("0.75")
},
{
"29" : NumberDecimal("0.18")
},
{
"30" : NumberDecimal("0.23")
},
{
"245" : NumberDecimal("0.26")
},
{
"277" : NumberDecimal("0.13")
},
{
"270" : NumberDecimal("0.19")
}
]
}
Method2: Working but wrong result
MongoCollection<Document> document = mongoTemplate.getCollection("booksCollection");
BasicDBObject query = new BasicDBObject();
query.put("_id", storeId);
BasicDBObject bookDiscount = new BasicDBObject();
bookDiscount.put(bookId, discount);
BasicDBObject update = new BasicDBObject();
update.put('$set', new BasicDBObject("books", bookDiscount));
document.findOneAndUpdate(query, update);
Method2 Output: Value always gets replaced
{
"_id" : "16644158",
"locationInfRate" : {
"2857" : NumberDecimal("0.68")
},
"_class" : "com.test.books"
}
What I learned:
$push method pushes the data as a member of a list.
$set method updates the data, but for maps, it replaces data if the map is not correctly formatted in the query.
I edited my $set query to below format and it worked:
MongoCollection<Document> document = mongoTemplate.getCollection("booksCollection");
BasicDBObject query = new BasicDBObject();
query.put("_id", storeId);
BasicDBObject bookDiscount = new BasicDBObject();
bookDiscount.put("books." + bookId, discount);
update.put('$set', bookDiscount);
document.findOneAndUpdate(query, update);
I have a collection "prefs" with document structure as below
{
_id: {
userId: "abc"
},
val: {
status: 1,
prefs: [
{
value: "condition",
lastSent: ISODate("2017-07-17T23:46:53.717Z")
}
],
deal: 2,
prevDeal: 3
}
}
I am trying to update the date field lastSent with a condition on userId and status. Below are the queries that I derieved from my Java code.
Select Query:
{ "_id" : { "userId" : "abc"} , "val.status" : 1 , "val.prefs.value" : "condition"}
Update Query:
{ "$set" : { "val.prefs.$.lastSent" : { "$date" : "2017-07-17T23:50:07.009Z"}}}
The above query is giving error as follows:
The dotted field 'prefs.$.lastSent' in 'val.prefs.$.lastSent' is not valid for storage.
How do I achieve this?
Below is my Java code:
BasicDBObject _idObject = new BasicDBObject();
_idObject.put("userId", "abc");
BasicDBObject _selectQuery = new BasicDBObject();
_selectQuery.put("_id", _idObject);
_selectQuery.put("val.status", 1);
_selectQuery.put("val.prefs.value", "condition");
BasicDBObject _valueUpdateQuery = new BasicDBObject();
_valueUpdateQuery.put("prefs.$.lastSent", lastSent);
BasicDBObject _updateQuery = new BasicDBObject();
_updateQuery.put("$set", new BasicDBObject("val", _valueUpdateQuery));
prefs.update(_selectQuery, _updateQuery, true, true);
I just tested with your code in mongo shell this codes works fine you don't have to mention
$date
and i used this code for updating date
db.getCollection('tester').update({ "_id" : { "userId" : "abc"} , "val.status" : 1 , "val.prefs.value" : "condition"},{ "$set" : { "val.prefs.$.lastSent" : new Date()}})
I am using the below mongo query to get the max temperature. Can any one help how to implement in java using mongo java driver using BasicDBObject and DBObject?
db.EventLog.aggregate(
[
{
$group:
{
_id: "$_id",
maxInnerTemp: { $max: { $concat : [ "0", "$fields.innerTemp"]}}
}
}
]
)
Use mongo java aggregation like below code ( not tested ) :
// $group operation
BasicDBList concat = new BasicDBList();
concat.add("0");
concat.add("$fields.innerTemp");
DBObject groupFields = new BasicDBObject("_id", "$_id");
groupFields.put("maxInnerTemp", new BasicDBObject("$max", new BasicDBObject("$concat", concat));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
List < DBObject > pipeline = Arrays.asList(group); AggregationOutput output = collectionName.aggregate(pipeline);
for (DBObject result: output.results()) {
System.out.println(result);
}
I want to retrieve id of a record in mongodb collection by specifying the name.
That's what I tried by it returned the whole thing, but I just want the id. Any help?
DBCollection parameterTable = db.getCollection("configurationTable");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "data1");
DBCursor cursor = parameterTable.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
And that's what it returns:
{ "_id" : { "$oid" : "5423d3ceccf213e6d265fc9c"} , "name" : "data1" , "created" : { "$date" : "2014-09-25T08:35:26.361Z"} , "timestamp" : { "$date" : "2014-09-25T08:35:26.361Z"}}
I'm not much sure, yet to get the id you can do something like :
cursor.next().get("_id");