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();
Related
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
Writing programm to Parse some data from one website using AngleSharp. Unfortunately I didn't find any documentation and it makes understanding realy hard.
How can I by using QuerySelectorAll get only link? I'm getting now just all things <a ...>...</a> with Name of article.
1. Name of artucle
The method I'm using now:
var items = document.QuerySelectorAll("a").Where(item => item.ClassName != null && item.ClassName.Contains("object-title-a text-truncate"));
In the previous example I also used ClassName.Contains("object-name"), but if we deal with table cells, there are no any class. As I understand to parse right element maybee I must use some info about parent also. So here is the question, how can I get this '4' value from tabble cell?
....<th class="strong">Room</th>
<td>4</td>....
Regarding your first question.
Here is an example that you can extract the link address.
This a Link of another Stackoveflow post that is related.
var source = #"<a href='http://kinnisvaraportaal-kv-ee.postimees.ee/muua-odra-tanaval-kesklinnas-valmiv-suur-ja-avar-k-2904668.html?nr=1&search_key=69ec78d9b1758eb34c58cf8088c96d10' class='object-title-a text-truncate'>1. Name of artucle</a>";
var parser = new HtmlParser();
var doc = parser.Parse(source);
var selector = "a";
var menuItems = doc.QuerySelectorAll(selector).OfType<IHtmlAnchorElement>();
foreach (var i in menuItems)
{
Console.WriteLine(i.Href);
}
For your Second question, you can check the example on the documention, here is the Link and below is the code sample:
// Setup the configuration to support document loading
var config = Configuration.Default.WithDefaultLoader();
// Load the names of all The Big Bang Theory episodes from Wikipedia
var address = "https://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
// Asynchronously get the document in a new context using the configuration
var document = await BrowsingContext.New(config).OpenAsync(address);
// This CSS selector gets the desired content
var cellSelector = "tr.vevent td:nth-child(3)";
// Perform the query to get all cells with the content
var cells = document.QuerySelectorAll(cellSelector);
// We are only interested in the text - select it with LINQ
var titles = cells.Select(m => m.TextContent);
I have to following code to query from dynamodb
Search search = table.Query(new QueryOperationConfig { Filter = filter, AttributesToGet = list of attributes});
I can see there is data in search by expanding its node while debugging, but could not find an easy way to get the items key and values directly.
I tried with
List<Document> documentSet = new List<Document>();
do
{
documentSet = search.GetNextSet();
foreach (var document in documentSet)
{
HttpContext.Current.Response.Write(document["columnName"]);
HttpContext.Current.Response.Write(document["columnName"]);
}
} while (!search.IsDone);
Is there any direct way to get the keys and value from Search object in json or table any in any thing?
Thanks
Given an individual Document (in your case, the document object), you can call document.ToJson() or document.ToJsonPretty() to retrieve the JSON representation of the document. This blog post provides more details.
Basically I have a single element inside of an xml file where I store settings for my application. This element mirrors a class that I have built. What I'm trying to do using LINQ, is select that single element, and then store the values stored inside of that element into an instance of my class in a single statement.
Right now I'm selecting the element seperately and then storing the values from that element into the different properties. Of course this turns into about six seperate statements. Is it possible to do this in a single statement?
It will be better if you can show your XML but you can get general idea from code below
XDocument doc = //load xml document here
var instance = from item in doc.Descendants("ElementName")
select new YourClass()
{
//fill the properties using item
};
You can use LINQ to XML, e.g.
var document = XDocument.Load("myxml.xml");
document.Element("rootElement").Element("myElement").Select(e =>
new MySettingsClass
{
MyProperty = e.Attribute("myattribute").Value,
MyOtherProperty = e.Attribute("myotherattribute").Value
});
See http://msdn.microsoft.com/en-us/library/bb387098.aspx for more details.
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.