mongodb c# pusheach position subdocument - c#

I am trying to use c# to insert each new subdocument into an array at the top position using driver version 2.4.2.
In mongo the following command works well:
db.getCollection('Operation').update(
{_id: ObjectId('586e9ec5ab3d05173cd88957') },
{$push: {'location': {$each: [ { 'value' : 'Site', 'time' : ISODate('2017-02-24T16:05:44.204Z'), 'user' : 'user1' } ], $position: 0 } } }
)
Then the result is:
{
"_id" : ObjectId("586e9ec5ab3d05173cd88957"),
"location" : [
{
"value" : "Site",
"time" : ISODate("2017-02-24T16:05:44.204Z"),
"user" : "user1"
}
]
}
But so far I do not succeed in getting the same result in C#. I have tried so far:
var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse("586e9ec5ab3d05173cd88957"));
var update = Builders<BsonDocument>.Update.PushEach("location", new List<BsonArray>() { new BsonArray { new BsonDocument { { "value", "Site" }, { "time", DateTime.UtcNow }, { "user", "user1" } } } }, position: 0);
collection.UpdateOne(filter, update);
And also no succes trying to specify all in text:
collection.UpdateOne("{ '_id': ObjectId('586e9ec5ab3d05173cd88957') }", "{ '$push': {'location': { '$each': [ { 'value' : 'Site', 'time' : ISODate('2017-02-24T16:05:44.204Z'), 'user' : 'user1' } ], $position: 0 } } }");
Any suggestions?

PushEach expects just a BsonArray parameter, not a List<BsonArray>.
You should be doing something like
var update = updateBuilder.PushEach("location", locationBSONArray);
There is no need to serialize to bson document.

Took me a while because of a typo. The answer of user1892538 was correct. This works:
var update = Builders<BsonDocument>.Update.PushEach("location", new BsonArray { new BsonDocument { { "value", "Site" }, { "time", DateTime.UtcNow }, { "user", "user1" } } }, position: 0);

Related

How to set field to null in Elasticsearch using C# NEST + script?

For example we have following document in elastic:
{
"name": "Bob",
"age": "22",
"phrase": "ohohoho",
"date": "2022-10-20T00:00:00Z"
}
string phrase ;
DateTime? date;
Then we want put following:
{
"name": "not Bob",
"age": "22",
"phrase": null,
"date": null
}
in c#:
var updateRequest = new UpdateRequest<T, T>(entity)
{
ScriptedUpsert = true,
Script = new InlineScript(
$"if (someCondition) {{ctx._source.putAll(params.entity);}} else {{ctx.op = \"noop\";}}")
{
Lang = "painless",
Params = new Dictionary<string, object>() { { "entity", entity } },
},
Upsert = Activator.CreateInstance<T>()
};
but in the end it will not update phrase and date.
It makes following request:
POST /myIndex/_update/b90278fd-1a66-40bf-b775-d076122c6c02
{
"script": {
"source": ""if (someCondition) {{ctx._source.putAll(params.entity);}} else {{ctx.op = \"noop\";}}"",
"lang": "painless",
"params": {
"entity": {
"name": "",
"age": 22
}
}
},
"upsert": {
"age": 0
}
}
Idk why but it skips all fields with null.
How to update nullable fields to null?
NEST does not support sending null values by default.
You can have a check in script such that if a value is not passed then you can remove it from document.
var updateRequest = new UpdateRequest<T, T(entity)
{
ScriptedUpsert = true,
Script = new InlineScript($"if (params.entity.phrase==null)ctx._source.remove('phrase');")
{
Lang = "painless",
Params = new Dictionary<string, object>() { { "entity", entity } },
},
Upsert = Activator.CreateInstance<T>()
};
You can check for more details here

How to use raw MongoDB aggregation query in C#?

I have the below mongoDB query working perfectly fine in mongoDB shell but wondering how to use that query in C#?
db.collection.aggregate([{
$match: {
fieldName: "dsdsd",
createdAt: {
$gte: ISODate("2021-07-05T12:29:30.000+00:00"),
$lte: ISODate("2021-07-15T12:29:30.000+00:00")
}
}
}, {
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d-%H",
date: "$createdAt"
}
},
items: {
$first: '$$ROOT'
}
}
},{"$replaceRoot":{"newRoot":"$items"}}
,{"$sort":{"createdAt":-1}}
])
I want to use the below raw query in c# something like below:
var pipeline = {
$match: {
fieldName: "dsdsd",
createdAt: {
$gte: ISODate("2021-07-05T12:29:30.000+00:00"),
$lte: ISODate("2021-07-15T12:29:30.000+00:00")
}
}
}, {
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d-%H",
date: "$createdAt"
}
},
items: {
$first: '$$ROOT'
}
}
},{"$replaceRoot":{"newRoot":"$items"}}
,{"$sort":{"createdAt":-1}}
var result = await _mongoDbContext.model.Aggregate(pipeline).ToListAsync();
you can add any custom stage via AppenStage
collection
.Aggregate()
.AppendStage<BsonDocument>(BsonDocument.Parse("stage1"))
.AppendStage<BsonDocument>(BsonDocument.Parse("stage2"))
..
or
var pipeline = new EmptyPipelineDefinition<BsonDocument>()
.AppendStage<BsonDocument, BsonDocument, BsonDocument>(BsonDocument.Parse("stage1"))
.AppendStage<BsonDocument, BsonDocument, BsonDocument>(BsonDocument.Parse("stage2"));
collection.Aggregate(pipeline).ToList();
UPDATE:
you can also use a shell-like syntax for db.runCommand (which is harder):
MongoDB Enterprise mongos> db.runCommand({ aggregate: 'test', pipeline: [ {stage1_json}, {stage2_json} ], cursor: {} })
...
where the c# equivalent is:
var result = db.RunCommand<BsonDocument>("{ aggregate : 'test', pipeline: [ {stage1_json}, {stage2_json} ], cursor: {} }");

$Cond and $Eq Inside $Project - C# Driver MongoDB

Im facing a litle problem in Deserializing BsonDocuments type, I couldn't find the reason, but I have some clues, maybe, it's about {0 fields}, because the error tel me about the column I have {0 fields}. I also realised that its a {} (empty array).
MY MONGO QUERY ON ROBO 3T AND THE RESULT
MY C# MONGO QUERY
var connString = "mongodb+srv";
var client = new MongoClient(connString);
var database = client.GetDatabase("Base");
var collection = database.GetCollection<BsonDocument>("collection");
var match1 = new BsonDocument("$match", new BsonDocument("PartnerId", "2021"));
var match2 = new BsonDocument("$match", new BsonDocument("CD_CLIENTE", codCond));
var project = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "CD_CLIENTE", 1 }, { "CD_ACESSO", 1 },
{ "ID_ACESSO", 1 },{ "NOME", 1 },{ "NU_TELEFONE", 1 }, { "EMAIL", 1 }, { "NU_KIPER_RF", 1 }, { "NU_KIPER_TAG", 1 },
{ "FG_KIPER_MOBILE", 1 },{ "KEY_HASH", 1 },}}}; MY MONGO AND RESULT
var sort = new BsonDocument("$sort", new BsonDocument("NOME", 1));
var pipeline = new[] { match1, match2, project, sort };
var result = collection.Aggregate<BsonDocument>(pipeline).ToList();
var lista = JsonConvert.DeserializeObject<List<UsuariosAcessos>>(result.ToJson());
The error is here:
var lista = JsonConvert.DeserializeObject<List<UsuariosAcessos>>(result.ToJson());
Exatly when I try to Deserialise an that "empty array" to Model int?. I found a work around, because I just need to know if, we have or dont not something in NU_KIPER_TAG and NU_KIPER_RF, so, I did this new Mongo query.
NEW MONGO QUERY USING $COND
db.dbACESSO.aggregate([
{
$match: { PartnerId: "2021", CD_CLIENTE: 4003}
},
{
$project: {_id:0, CD_CLIENTE:1, CD_ACESSO:1, ID_ACESSO:1, NOME:1, NU_TELEFONE:1,EMAIL:1, FG_KIPER_MOBILE:1,
TAG:{$cond: [{ $eq: [ "$NU_KIPER_TAG", {}]}, 0, 1 ]}, CONTROLE:{$cond: [{ $eq: [ "$NU_KIPER_RF", {}]}, 0, 1 ]},
APPATIVO:{$cond: [{ $eq: [ "$KEY_HASH", {}]}, "", "$KEY_HASH" ]}}
}
])
I couldnt translate it to C#, I tried hard, but i'm not familiarized with the sintaxe. I also googled for a sample with no success.
I think its something like:
var project = new BsonDocument {
{
"$project", new BsonDocument { { "_id", 0 }, { "CD_CLIENTE", 1 }, { "CD_ACESSO", 1 },{ "ID_ACESSO", 1 },{ "NOME", 1 }
,{ "NU_TELEFONE", 1 }, { "EMAIL", 1 }, { "NU_KIPER_RF", 1 }, { "NU_KIPER_TAG", 1 },{ "FG_KIPER_MOBILE", 1 },{ "KEY_HASH", 1 },
{"TAG", new BsonDocument{{"$cond", new BsonDocument {{ "$eq", "$NU_KIPER_TAG", "{}"}}, 0, 1 ]}, } }
}
}
};
Try this way, it will work!
var project = new BsonDocument
{
{
"$project", new BsonDocument
{
{ "_id", 0},
{ "CD_CLIENTE", 1},
{ "CD_ACESSO", 1 },
{ "NOME", 1},
{ "EMAIL", 1 },
{ "FG_KIPER_MOBILE", 1 },
{ "GRUPO", "$GRUPO.NM_DESCRICAO" },
{ "UNIDADE", "$UNIDADE.NM_DESCRICAO" },
{ "NU_TELEFONE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_TELEFONE", new BsonDocument { } } }}, "","$NU_TELEFONE" } }}},
{ "TAG", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_KIPER_TAG", new BsonDocument { } } }}, 0,1 } }}},
{ "CONTROLE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_KIPER_RF", new BsonDocument { } } }}, 0,1 } }}},
{ "APPATIVO", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$KEY_HASH", new BsonDocument { } } }}, "", "$KEY_HASH" } }}},
}
}
};
The empty array will be represented for: new BsonDocument { }
{ "NU_TELEFONE", new BsonDocument{{ "$cond", new BsonArray{new BsonDocument{{"$eq", new BsonArray{ "$NU_TELEFONE", new BsonDocument { } } }}, "","$NU_TELEFONE" } }}},
What about using the fluent C# like this:
var connString = "mongodb+srv";
var client = new MongoClient(connString);
var database = client.GetDatabase("Base");
var collection = database.GetCollection<UsuariosAcessos>("collection"); //Here you put you Model
var filter = Builders<UsuariosAcessos>.Filter.Eq(x => x.PartnerId, cliente)
& Builders<UsuariosAcessos>.Filter.Eq(x => x.CD_CLIENTE, codCond);
var lista = collection.Aggregate().Match(filter).Project(x => new UsuariosAcessos
{
CD_CLIENTE = x.CD_CLIENTE,
ID_ACESSO = x.ID_ACESSO,
CD_ACESSO = x.CD_ACESSO,
NOME = x.NOME,
NU_TELEFONE = x.NU_TELEFONE,
EMAIL = x.EMAIL,
NU_KIPER_RF = x.NU_KIPER_RF,
NU_KIPER_TAG = x.NU_KIPER_TAG,
FG_KIPER_MOBILE = x.FG_KIPER_MOBILE,
KEY_HASH = x.KEY_HASH
}).ToList();
About your problem at Deserialization time, I think it's not possible to translate {} (empty array) to int or int?, it's appear to be the reason your deserialization is not working, try to change your model.
FROM:
public int? NU_KIPER_TAG { get; set; }
public int? NU_KIPER_RF { get; set; }
TO:
public object NU_KIPER_TAG { get; set; }
public object NU_KIPER_RF { get; set; }
Its not you are looking for, but, maybe your Deserialization are going to Work. After that, you can convert the data. Hope this helps.

BSON error creating new BsonDocument for MongoDB

I'm new to MongoDB and, after some tutorials, I'm trying to read data from my SQL Server database and reverse them in MongoDB, with C# and Entity Framework.
I found this code on MongoDB site:
async static void addDoc()
{
var document = new BsonDocument
{
{ "address" , new BsonDocument
{
{ "street", "2 Avenue" },
{ "zipcode", "10075" },
{ "building", "1480" },
{ "coord", new BsonArray { 73.9557413, 40.7720266 } }
}
},
{ "borough", "Manhattan" },
{ "cuisine", "Italian" },
{ "grades", new BsonArray
{
new BsonDocument
{
{ "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "A" },
{ "score", 11 }
},
new BsonDocument
{
{ "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
{ "grade", "B" },
{ "score", 17 }
}
}
},
{ "name", "Vella" },
{ "restaurant_id", "41704620" }
};
var collection = _database.GetCollection<BsonDocument>("restaurants");
await collection.InsertOneAsync(document);
}
It works as I expect. So, I made this:
using (var db = new Entities())
{
foreach (var trans in db.TRANSACTIONS)
{
try
{
var document = new BsonDocument
{
{ "ID", erog.ID.ToBson() },
{ "CUSTOMER" , new BsonDocument
{
{ "CSTID", trans.CUSTOMERS.CSTID.ToBson() },
{ "NAME", trans.CUSTOMERS.NAME.ToBson()},
{ "CITY", trans.CUSTOMERS.CITY.ToBson() },
{ "ZIP", trans.CUSTOMERS.ZIP.ToBson() },
{ "STATE", trans.CUSTOMERS.STATE.ToBson() },
}
},
{ "TRANSACTIONNUMBER", trans.TRANSACTIONNUMBER.ToBson() },
{ "TIMESTAMP", erog.TIMESTAMP.ToBson() },
{ "AMOUNT", erog.AMOUNT.ToBson() },
{ "PAYMENT", erog.PAYMENT.ToBson() },
};
var collection = _database.GetCollection<BsonDocument>("transactions");
collection.InsertOne(document);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
When I try to execute this code, I get an error when executing var document = new BsonDocument {....}; the error is "A string value cannot be written to the root level of a bson document". So, as you can see, I tried to put the .ToBson() at the end of my values, but the result is still the same.
The only difference is the value isn't a string between two quotation marks, but it is a real value from my table.
How can I reach my goal and create&insert a document in my MongoDB database, starting from my code above?
Ok, dumb question, dumb solution.
Just replace all toBson() with toString() and now it works! :)

Convert Single column C# DataTable to JSON using JSON.NET

The DataTable contains single column EFFDATE.
EFFDATE
2015-06-15
2014-10-21
2014-07-17
2014-07-16
2014-06-17
2014-03-13
I have the following code to convert DataTable dttbl to JSON.
JsonConvert.SerializeObject(dttbl, Formatting.Indented)
The output from conversion is
[
{
"EFFDATE": "2015-06-15"
},
{
"EFFDATE": "2014-10-21"
},
{
"EFFDATE": "2014-07-17"
},
{
"EFFDATE": "2014-07-16"
},
{
"EFFDATE": "2014-06-17"
},
{
"EFFDATE": "2014-03-13"
}
]
The output I want is
{
"EFFDATE": [
"2015-06-15",
"2014-10-21",
"2014-07-17",
"2014-07-16",
"2014-06-17",
"2014-03-13"
]
}
Please advice.
var json = JsonConvert.SerializeObject(
new { EFFDATE = dt.AsEnumerable().Select(r => r[0]) }
);

Categories