I would like to know how can I check the existence of two objects with mongoDB and C#.
I know how to do it for 1 object:
foreach (BsonDocument item in collection.Find(Query.Exists("Boiling point")))
But i have no idea how to check on two objects.
Thanks!
You can use Query.And to search more than one condition.
foreach (BsonDocument item in collection.Find(
Query.And(
Query.Exists("Boiling point"),
Query.Exists("Freezing Point")))
A complete list is available in the online documentation here.
Related
I'm trying to access all documents of the same collection that have a specific field value.
var docs = await CrossCloudFirestore.Current
.Instance
.Collection("myCollcetion")
.WhereEqualsTo("field", variable)
.GetAsync();
I tried to run foreach for the docs but it doesn't work. How do I access each document in order to get the values of other fileds in them?
Based on Jason's suggestion foreach should be run for docs.Documents.
i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like:
foreach (var item in list.Skip(1).TakeTheRest())
{....
I played around with TakeWhile , but was not successful. Probably there is also another, simple way of doing it?
From the documentation for Skip:
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
So you just need this:
foreach (var item in list.Skip(1))
Just do:
foreach (var item in input.Skip(1))
There's some more info on the MSDN and a simple example that's downloadable here
Wouldn't it be...
foreach (var in list.Skip(1).AsEnumerable())
Say, I have collection People. How should I fetch first 1000 documents that doesn't have a field Phone? As I understand, I should use $exists however I cannot understand how to use it from .NET driver and there is next to no info on that topic on the internet. Any help will be appreciated. Thanks!
Assume your Model Class is Model and colelction name is "Model".
var coll = db.GetCollection<Model>("Model");
var ret = coll.Find(Builders<Model>.Filter.Exists(d => d.Phone, false))
.Limit(1000)
.ToList();
With ToList you will get already loaded list, sometimes it's better to use ToEnumerable and have enumerable to iterate.
I am learning about RSS feeds and I have hit a problem which I am surprised I have never had before now, it is probably a very simple answer but I can't find anything on Google. I am using SyndicationItem to get the XML items from an RSS feed but when you use foreach it takes every item, I only want to get the latest item - the code snippet is below.
//Add required parsing in here
foreach (SyndicationItem item in feed.Items)
{
//put data you want parsed
}
Obviously I have thought of just adding a break at the end, but this is definitely something I will need to find an answer for in the future and I would like to resolve it much cleaner than that.
So any help here would be appreciated, thanks!
Try this:
var latest = feed.Items.OrderByDescending(x=>x.PublishDate).FirstOrDefault();
OrderByDescending will order items by date descending and taking FirstOrDefault item will get item with highest publish date (latest item)
SyndicationItem has PublishDate property
Read more about Linq it's very usefull creating such queries.
EDIT:
if they are sorted by PublishDate you can use LastOrDefault method, but if you are not 100% sure about that it's better to sort it before.
var latest = feed.Items.LastOrDefault();
EDIT2:
handle empty lists like:
if(!feed.Items.Any)
{
//alert about no elements
}
i just started learning how lucene works and am trying to implement it in a site i allready wrote with mysql.
i have a field named city in my documents, and i want to get all the values for city from the documents.
i have found this question ( which is exactly what i need ) Get all lucene values that have a certain fieldName
but all they show there is a line of code, and as i said, i am not experienced enough to understand how to implement that.
can someone please help me with some code to implement IndexReader.Open(directory,true).Terms(new Term("city", String.Empty));
what comes before / after that declaration ?
i have tried this:
System.IO.DirectoryInfo directoryPath = new System.IO.DirectoryInfo(Server.MapPath("LuceneIndex"));
Directory directory = FSDirectory.Open(directoryPath);
Lucene.Net.Index.TermEnum iReader = IndexReader.Open(directory,true).Terms(new Term("city", String.Empty));
but how do i iterate over the results?
This loop should iterate over all the terms:
Term curTerm = iReader.Term();
bool hasNext = true;
while (curTerm != null && hasNext)
{
//do whatever you need with the current term....
hasNext = iReader.Next();
curTerm = iReader.Term();
}
I'm not familiar with C# API, but it looks very similar to the Java one.
What this code does is to get an instance of IndexReader with read-only access that is used to read data from Lucene index segments stored in directory. Then it gets an enumeration of all terms, starting at the given one. Dictionary (index part that stores the terms) in Lucene is organized .tis files, ordered lexicographically first by field name and then by term text.
So this statement gives you an enumeration of all term texts, starting at the beginning of the field city (besides: in Java you would rather write new Term("city")). You now need to find out the C# API of this enumeration, and then walk through it till you will get a Term that have something different as the field().
A final note: generally, you should avoid doing things like this: it may for example limit your ability to distribute the index. If it turns out that this is a thing that you are doing at the very beginning of using Lucene, then it's probable that you are using it more like a document database than a search library.