Insert document inside another document - c#

how can I insert a document inside another document with mongodb ?
I've tried something like the code below but I always have a problem converting my list of setting in the field Settings:
var settingViewModels = new[]{ setting };
var query = Query.EQ("Name", applicationName);
var update = Update.AddToSet("Settings", BsonArray.Create(setting));
db.Collection<Applications>().Update(query, update, UpdateFlags.Upsert, SafeMode.True)
I got an error when I convert my settingViewModels into BsonArray saying:
.NET type MyProject.SettingViewModel cannot be mapped into BsonType.Array
Parameter name: value
Any idea ?
thanks
John

If you want add setting item to the Settings[] array of Application collection you should use ToBsonDocument() extention method from MongoDB.Bson namespace:
var update = Update.AddToSet("Settings", setting.ToBsonDocument());
You no need create BsonArray.

Related

MongoDB search using text index not working c#

I have Added text index on my collection. I am trying to filter the data with text search with some additional filters. But It is not working well with other filters.
{$text:{$search:"test"},Type:"5"}
The above query returns all 42 entries matching the criteria from mongoDB Atlas.
But when I am doing this from c# I think I am querying it wrong. What am I missing here.
var collection = db.GetCollection<TestTbl>("TestTbl");
var filter = Builders<TestTbl>.Filter.Text(searchtext)
&Builders<TestTbl>.Filter.Eq("TypeID", TypeID);
var data = collection.Find(filter).ToList();
here data is returned null.
When I am giving text only in the filter it works fine.
{$text:{$search:"test"}}
var collection = db.GetCollection<TestTbl>("TestTbl");
var filter = Builders<TestTbl>.Filter.Text(searchtext);
var data = collection.Find(filter).ToList();
The Error was with my model it was Guid Type forgot to mention the bsontype string for the field representation

How do I a grab a parameter within a document MONGODB (C#)

i have the following problem, i filtered the collection to get the specific document in collection which i need. The document inside it has some variable with some values. How do i grab the specific variable and its value from the specific document.
MongoClient client = new MongoClient();
var db = client.GetDatabase("myfirstdb");
var collection = db.GetCollection<PlayerInfo>("players");
var filter = Builders<PlayerInfo>.Filter.Eq("playerName", player.Name);
//find in document filter the parameter "isAdmin" and grab its value.
This is how my document looks like.
You have to use Project clause when you perform the find operation on the filter. Below code will do.
bool isAdmin = collection.Find(filter).Project(x => x.isAdmin).FirstOrDefault();

How to use "Filter" method in AngleSharp?

I tryed to use filter like this:
var table = HTML.QuerySelector(".table>tbody");
var rows = table.QuerySelectorAll<IHtmlAnchorElement>("tr").Filter("td>div>input[value*=\"LT\"]");
But it just gave me an empty collection. I expected it chooses all tr where exist "td>div>input[value*="LT"]". I checked with browser that there's no css-selector mistakes: I run this code in the console and it worked as expected:
[...document.querySelector(".table>tbody").querySelectorAll("tr")].filter((tr) => tr.querySelector('td>div>input[value*="PC"]'));

How to use C# Mongodb driver typed methods Update fields of array document elements

Using the MongoDB C# Driver version 2.0.1 with Mongodb 3.0, is it possible to use typed methods to update an array field document element?
For example, I have the following document:
{
Name:"Ken",
ContactNo:[ { Number:"123", Type:"Mobile"},
{ Number:"456", Type:"Office"},
{ Number:"531", Type:"Fax"}
]
}
How do I do the following operations using typed C# methods:
1) Update the Type field for all elements of the ContactNo array to be "PABX"
2) Update ContactNo array document element's Type field whose Number field equals "123" to be "Fiber"
3) Update the first element of the Contact array and set its Type field to be "Unknown"
It's not currently possible to update all the items in an array using the positional operator. See this StackOverflow question and this MongoDB issue. However, if you know the number of elements in your array ahead of time (or can obtain it somehow), then this will work:
var numberOfElementsInArray = 3;
var filter = Builders<Contact>.Filter.Eq("Name", "Ken");
var update = Builders<Contact>.Update.Combine(Enumerable.Range(0, numberOfElementsInArray)
.Select(i => Builders<Contact>.Update.Set("ContactNo." + i + ".Type", "PABX")));
collection.UpdateOneAsync(filter, update).Wait();
This code will set the Type property of the element in the ContactNo array with a Number of 123 to Fiber:
var filter = Builders<Contact>.Filter.And(
Builders<Contact>.Filter.Eq("Name", "Ken"),
Builders<Contact>.Filter.Eq("ContactNo.Number", "123"));
var update = Builders<Contact>.Update.Set("ContactNo.$.Type", "Fiber");
collection.UpdateOneAsync(filter, update).Wait();
This code will set the Type property of the first element in the ContactNo array to Unknown:
var filter = Builders<Contact>.Filter.Eq("Name", "Ken");
var update = Builders<Contact>.Update.Set("ContactNo.0.Type", "Unknown");
collection.UpdateOneAsync(filter, update).Wait();
Note that all of this code assumes that you have a class called Contact that corresponds to the data you specified in your question (your actual class might be called something else, I just called it Contact here), and that collection is an instance of IMongoCollection<Contact>.
For example:
var client = new MongoClient("mongodb://localhost:27017");
var collection = client.GetDatabase("your database").GetCollection<Contact>("your collection name");

Pass Array to a WCF function

So, I have a C# project in which, I am loading a XML document (contains name of students and Id) using Linq to xml and I have to get the associated data (their due date, amount and stuff ) from a WCF service. I added the service with just right click and add service reference and now need to pass arrays to the GetData function, which I initialized but its null obviously. I cant able to convert my array to service type and the function returns array too. How do I assign the array to studentArray ?
ServiceReference1.ServiceClient client = new ServiceReference1.RycorServiceClient();
Application.ServiceReference1.Student[] studentArray = new ServiceReference1.Student[9];
Student[] array = studentList.ToArray();
//for (int i = 0; i <= array.Count(); i++)
// studentArray[i] = (RycorApplication.ServiceReference1.Student)array[i];
//this gives me an error says Cannot convert type 'Application.Student' to 'Application.ServiceReference1.Student'.
var data = client.GetData(studentArray);
After getting this data, how do I save this data to my XML file ?
You are getting this error because Application.Student is a different type, you can try to use Application.ServiceReference.Student to save the list of students instead of the "studentList" type.
I suppose that "studentList is an "Application.Student" list and you have to use the same model or make a copy between them using something like this (in the first answer):
Copy values from one object to another
You pretty much have to do this:
List<ServiceReference1.Student> wcfStudentList = new System.Collections.Generic.List<ServiceReference1.Student>();
foreach (var student in studentList)
{
wcfStudentList.Add(new ServiceReference1.Student()
{
ID = student.ID,
Name = student.Name,
..etc..
});
}
var data = client.GetStudentData(wcfStudentList.ToArray());
I do have to question why you don't just change the WCF call if you can to take a List of the student IDs instead of passing the entire object though?

Categories