Morphia Upserting Field as int64 instead of in32 - mongodb

I've written an operation upsert a document to Mongo using Morphia.
I have a field which I want to save as an int32, but after the upsert it is inserted as int64.
I have made sure that I convert the long as an int using Long.intValue() and the object that Morphia serializes back to, the member field is int. I have also checked the UpdateOperations.ops to see what morphia is upserting.
The upsert operation is:
UpdateOperations<Test> ops = datastore.createUpdateOperations(Test.class)
.set("test_field", testField.intValue())
The current version of Mongo I am using is 3.0.
Any help would be appreciated!
Thanks!
EDIT:
Looking at the update query operation in Morphia it is:
{$set={test_field=11}}

I managed to find out the solution. The query for the upsert was querying on the test_field as a long rather than an int. Mongo 3.0 sees this as the type to insert - however running on Mongo 3.2, there is no issue and it will upsert with the type specified in the upsert operation, not the query.

Related

MongoDb database Filter is not working in Compass

When I query all the records, i get the results as shown below.
When i copy an ID from the result and add it in the filter, no result is found.
Here is the example filter from documentation
What am i doing wrong? mongo db got to be kidding me :/
You are not providing the data type with the query. Since your _id field is ObjectId type and you are comparing as string that is why it is not working.
Change your filter condition and convert your string id to ObjectId type.
{_id: ObjectId('yourId')}
MongoDB provides an automatic unique identifier for the _id field in the form of an ObjectId data type.

Return single document in mongo aggregation in Go driver

I am using official mongo driver for Golang: go.mongodb.org/mongo-driver/mongo
Preface
In this driver, I could not found any method for returning one single object from aggregation query.
driver aggregation documentation
Problem
The problem I am facing with this is if I have some documents which should be filtered and only first one should be returned, then I forcefully need to get all documents and return document on 0 index. In my knowledge, this is not optimized.
I have only one method for aggregation here, which returns cursor of multiple objects:
Is it possible to get single object in aggregation in this driver ?
Aggregation always returns a list of documents, but you may use the $limit stage to only return one document.
bson.M{"$limit": 1}

What should be the query for delete field from mongo db collection on groovy language

What should be the query for delete field from mongo db collection on groovy language
I am looking to delete parameter from collection.
normal mongo db query its working
db.users.updateOne({"userId.name":"LastName"},{$unset:{nationality:"",occupation:"",friendlyName: ""}});
but using groovy it doesn't
collection.deleteMany(eq("userId.name", "Lastname"),combine(set("nationality", ""),set("occupation", ""),set("friendlyName", "")));
with this query it doesnt work.
You don't need this combine bit as for the user removal you don't need to unset the nationality, occupation and friendlyName
Consider using MongoCollection.deleteOne() function and provide only userId.name attribute like:
def deleteResult = collection.deleteOne(eq("userId.name", "LastName"))
More information: MongoDB Performance Testing with JMeter

Update a MongoDB document in JAVA without explicit conversion

I am using the mongo-db java driver 3.8 and work with the collection as follows:
MongoDatabase md=mongoClient.getDatabase(databaseName);
MongoCollection<ConstructionPlan> collection=md.getCollection(plansCollectionName,abc.class);
collection.insertOne(item);
collection.find(Filters.eq("itemId", id),abc.class).first();
With this code I do not have to do any conversion. I was looking for a way to update an document the same way. I am thinking on something like this:
abc anABCObject=collection.find(Filters.eq("itemId", id),ConstructionPlan.class).first();
//updates...
collection.update(anABCObject);
Is there a way to update an existing document without BSON conversion? (I was unable to find it....)
updateOne for updating document fields using update operators.
You need replaceOne which takes the replacement document.
collection.replaceOne(
Filters.eq("itemId", id),
anABCObject
);

Morphia/MongoDb, Using and Index with FindAndModify()

I am currently in the process of refactoring some of our mongo querying code to get rid of methods deprecated in Morphia 1.3. I am running into an issue when trying to run a find and Modify query with an index . At the moment our legacy code is setting an index on the query in this fashion:
ds.createQuery(Entity.class).hintIndex("index")
I have removed it to be in line with morphia 1.3 and the code now looks something like this :
new FindOptions.modifier("$hint", "index");
return query.asList(findOptions);
As a result we do not set the index on the query until actually running it. This is fine of get queries, less so for upsert ones.
To replace the deprecated findAndModify : findAndModify(Query<T> query, UpdateOperations<T> operations, boolean oldVersion, boolean createIfMissing), morphia is asking to use findAndModify(Query, UpdateOperations, FindAndModifyOptions)
In this case, I would have expected to have a modifier method in the FindAndModifyOptions object to be able to set an index, when running the query. I can't seem to be able to do something like this:
FindAndModifyOptions findOptions = new FindOptions();
findOptions.modifier("$hint", SUGGESTION_CONTENT_TYPE_INDEX_NAME);
return datastore.findAndModify(query, updateOperations, findAndModifyOptions);
Can someone advise on how to pass the index? Is it possible to do it via the FindAndModifyOptions object ?if not, what would be the best way to set the index for the upsert query