Partial Update on MongoDB Error - mongodb

I'm trying to have a partial update on one of my documents in MongoDB, using C# driver. I've followed the following posts:
How do you update multiple field using Update.Set in MongoDB using official c# driver?
Partial mongodb upsert using the c# driver?
I get the following error when trying to do the update: "Only classes can be mapped currently", in the AutoMapper CreateClassMap class, the type received is System.Collections.Generic.IEnumerable`1[[MongoDB.Bson.BsonElement, MongoDB.Bson]], where it cannot be an interface.
The code I'm using is:
public void UpdateObjectByFields<T>(int id, Dictionary<string, object> modifiedFields)
where T : class
{
var collection = m_MongoDatabase.GetCollection<T>();
var builder = new UpdateBuilder();
foreach (var modifiedField in modifiedFields)
{
builder.Set(modifiedField.Key, modifiedField.Value.ToString());
}
collection.Update(Query.EQ("_id", id), builder);
}
where the T type is a valid collection in the Mongo.
What am I doing wrong?
Thanks,
Nir

Got this to work, apparently I was using some old dlls for the C# driver.
Fixed it by using the dlls from here:
https://github.com/mongodb/mongo-csharp-driver/downloads
Thanks,
Nir.

Related

Adding to each element of a Flux a List attribute obtained from a MongoDB reactive query

First time posting, I am learning reactive streams in Spring and I got the following problem.
I have a mongo repository for the class Workspace and another repository for the class Model.
When I query for all the workspaces, I get a Flux and I would like for each of the elements in this flux to be converted to a new class called WorkspaceDTO and make a query to the modelsRepository to retrieve all the models which attribute workspaceName is equal to my workspace name, adding this result to an attribute of WorkspaceDTO which is a List
I have been trying some things but haven't figured a way of doing this, any suggestions?
Finally managed to do it with this code:
Flux<Workspace> workspaces = repository.findAll();
return workspaces
.map(ws -> {
Mono<List<Model>> workspaceModels = modelRepository.findByWorkspaceName(ws.getName()).collectList();
return Mono.zip(Mono.just(ws),workspaceModels)
.map(t -> new WorkspaceDTO(t.getT1(),t.getT2()));
})
.flatMap(Flux::concat) //This operation transform a Flux<Mono<WorkspaceDTO>> into Flux<WorkspaceDTO>
.log();

Grails 3.1 - Can't find codec for domain class

I am not able to convert domain class into Basic DB object.
Below is my code:
def update_val
class_object.class.withNewSession { MongoCodecSession m ->
update_val = m.pendingUpdates.find {
it.key.name == d.class.getName()
}.value[0]nativeEntry.regions[0]."${instance.getDbKey()}"[0]
}
On below findOneAndUpdate function, I am getting error: "Can't find a codec for class class.domain". updateVal is returning as Domain Class object.
ClassName.class.findOneAndUpdate(new BasicDBObject(findVal), new BasicDBObject(updateval))
I am converting it from Grails 3.0 to Grails 3.1, here nativeEntry is returning as a domain class while in previous version, nativeEntry is returning as BasicDBObject.
Any solution?
I am using Grails 3.1 with gorm 5.0 and mongodb 3.4
I have resolved it. Add below solution to Application.yml
grails:
mongodb:
engine: mapping
It will convert MongoCodecSession to the previous MongoSession.
As in codecs, objects are no longer converted first to MongoDB Document objects and then to Groovy objects, instead the driver reads Groovy objects directly from the JSON stream at the driver level, which is far more efficient than the previous MongoSession.

How to filter documents in MongoDB and Spring boot

I am creating a Spring boot where I have "articulos" documents. I want to retrieve them from the DB depending on their fields value but I can not seem to achieve it with the examples I found in this website or anywhere else. This is because I see someone people use mongoTemplate which I assume is the interface class I have created for my repository but when I try to use that it says the method wasn't found.
This is what I am trying to do:
Query query = new Query();
query.addCriteria(Criteria.where("name").ne("Eric"));
List<Articulo> articulos = this.articuloRepository.find(query, Articulo.class);
articulosRepository is just and empty interface with all the annotations needed
Create the below repository method in ArticulosRepository
List<Articulo> findByNameNot(String name);
Use like
List<Articulo> articulos = this.articuloRepository.findByNameNot("Eric");
In your repository interface create a named query with your property like below if you have name in your document then method will be:
List<Articulo> articulos = findByName(String name);
Call this from your service by #Autowired repository.

MongoDb toArray

I am using mongodb and in the following code I am using mongodb find().toArray(), but it is giving me the error "Cannot read property 'toArray' of undefined"
req.activedb.collection('items').find().toArray(function (err, data) {
//...some code
})
whereas when I am using findOne(), then it is working properly.
req.activedb.collection('items').findOne(function (err, records) {
console.log(err, records); //Getting a single record here
})
req.activedb is my current db instance
Can you please tell me, what is missing here ?
I have resolved this issue.
Actually I was using mongoose to connect with my db, so it does not support find(). So now I am connecting to the db using new Db() method and it is working properly.
I have run belows command its work for me.
posts is nothing but collection.
db.posts.find().toArray();
You might be, you are making mistake in syntax .

Npgsql support for getting jsonb field as stream

I am trying to do the following on a jsonb field:
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
using (var results = reader.GetStream(0))
{
results.CopyTo(stream);
}
}
reader.Close();
}
and getting an exception telling me that GetStream is not supported for this field.
Am I missing something here or is it Npgsql that simply doesn't support streaming jsonb fields (yet)?
This is already handled in the next version of Npgsql, 3.1. Unfortunately that version is still in alpha, hopefully I'll be able to release a beta in around two months.
In the meantime you can work around this by reading the jsonb as a string, and if needed, wrapping a TextReader around it (may not be as efficient but will work).