MongoDB Insert Entity vs a BsonDocument using via C# Driver - mongodb

I am using the .NET MongoDB driver to insert values into my collection in MongoDB .
I noticed when I insert an entity the objects get stored as simple columns like in the second document below where as when I used a BsonDocument to insert documents they get inserted as an object under the _v column in the first document below.
Can someone explain whats the difference between the two?
Also is it possible to insert the BsonDocument like in the second document via the .NET driver? In my case I have to build the document Dynamically since I dont have a concrete entity to insert with.

The result you're seeing is related to dynamic handling. Mongo driver doesn't know which type you use and save this information in _t field. To fix it you just need to create collection based on BsonDocument directly like:
var coll = db.GetCollection<BsonDocument>("coll");
coll.InsertOne(new BsonDocument("name", "value"));
See this doc

So with the .NET driver:
If I insert the bson object directly the document is inserted as an object with column "_v"
If I insert the bson object after deserializing it using the BsonSerializer it can be inserted as simple columns in MongoDB.
var collection = database.GetCollection<dynamic>(collectionName);
var bson = new BsonDocument();
...
// Fill bson object
...
collection.InsertOne(bson); // Image 1
collection.InsertOne(BsonSerializer.Deserialize<dynamic>(bson)); // Image 2

Related

Postgres/jOOQ replace jsonb[] element

I'm having a Spring application with jOOQ and Postgresql database having a table (issues) with the following two columns:
id (Long)
documents (jsonb[]) <- array of jsonb (not jsonb array)
The document json structure is on the following format:
{
"id": (UUID),
"name": (String),
"owner"; (String)
}
What I want to achieve is to be able to replace documents with matching id (normally only one) with a new document. I'm struggling with the jOOQ or even the plain SQL.
I guess I need to write some plain SQL in jOOQ to be able to do this but that is ok (to a minimum). I had an idea to do the following:
Unnest the document column
Filter out the document that should be updated of the array
Append the document that should be updated
Store the whole array
Raw SQL looks like this but missing the new document to be added:
UPDATE issues SET documents = (SELECT ARRAY_AGG(doc) FROM issues, UNNEST(issues.documents) AS doc WHERE doc->>'id' != 'e4e3422f-83a4-493b-8bf9-37980d532538') WHERE issues.id = 1;
My final goal is to write this in jOOQ and append the document to be replaced. I'm using jOOQ 3.11.4.
You should be able to just concatenate arrays in PostgreSQL:
UPDATE issues
SET documents = (
SELECT ARRAY_AGG(doc) || '{"id":"e4e3422f-83a4-493b-8bf9-37980d532538","name":"n"}'::jsonb
FROM issues, UNNEST(issues.documents) AS doc
WHERE doc->>'id' != 'e4e3422f-83a4-493b-8bf9-37980d532538'
)
WHERE issues.id = 1
Some common array functions will be added to jOOQ in the near future, e.g. array concatenation, but you can get away for now with plain SQL templating I suspect?

How to insert data using insertMany() with DataType NumberDecimal in mongodb?

I have collection with field price that is of the DataType Decimal. When I use insertMany, how to set it to store in decimal type ?
let data=[{id:'1', price:10}, {id:'2', price:20}]
insertMany(data)
I user meteor and mongo node driver.
To insert multiple document in MongoDB collection you can use the insert method of MongoDB driver for nodejs. Meteor provides rawCollection method to access to the the Collection object corresponding to the collection from the MongoDB driver module which is wrapped by Mongo.Collection.
In your case the query will be like this:
let data=[{id:'1', price:10}, {id:'2', price:20}]
CollectionName.rawCollection().insert(data);

Can I get inserted record immediate after collection.InsertOneAsync(document) in MongoDB

We are using MongoDB framework in C# for DB calls.
When we insert new document in collection, we want updated result with inserted _id into database.
After you insert document using official C# driver, you already have it with updated _id column. Inserted document is updated by default.

MongoDB Java Driver creating Database and Collection

i was testing how to create database and collection mongo java driver.
MongoClient client = new MongoClient("localhost",27017);
DB db = client.getDB("ow");
DBCollection collection = db.getCollection("documents");
collection.save(new BasicDBObject("_id",1));
collection.remove(new BasicDBObject("_id",1));
boolean result = db.collectionExists("documents");
assertTrue(result);
assertNotNull(collection);
client.close();
I would prefer to use createCollection method on the DB object, but found that it does not create database / collection unless the first document is inserted.
My question is is this understanding correct ? Is above code correct was of creating collection or database.
prefer to use createCollection method on the DB object, but found that it does not create database / collection unless the first
document is inserted.
MongoDB creates a collection implicitly when the first document is saved into a collection. The createCollection() method explicitly creates a collection only and only if an options object is passed to it as an argument.
Now this makes sense. The options parameter can take in one or more arguments to decide the characteristics of the collection we want to create such as capped,autoIndexId,size,usePowerOf2Sizes,max no. of documents.
If we do not specify any of these options, the default behavior would take precedence, i.e create a collection lazily whenever the first insert is made, with default settings.
So if we want a collection whose characteristics we are going to define, then we can pass these characteristics as a DBObject to the createCollections() method and our collection would be created. Below is an example of how to pass the options.
BasicDBObject options = new BasicDBObject();
options.put("size", 12121212);
db.createCollection("hello", options);
Is above code correct was of creating collection or database.
Yes. It allows mongodb to apply the default configuration for your collection. Unless you want to set the
max,size,autoIndexId,capped,usePowerOf2Sizes properties for your new collection, this is fine.
Refer: http://docs.mongodb.org/manual/reference/method/db.createCollection/

Spring Data MongoDB return object converter

I have a large documents with 50 different fields in it. Only few fields are required when I save this document. When I fetch that document though Spring Data MongoDB is it possible to eliminate null value fields from the object?
During the save Spring Data MongoDB does the same thing and only save those values which are not null.
Our Query class has a fields() method which returns a Field object that allows defining which fields to be read from the document (Javadoc). Thus when executing the query object against a collection you get partial document back and thus a partially filled domain object.