Get All 'documents' from MongoDB 'collection' - c#

I need to retrieve all the documents that are in my collection in MongoDB, but I cannot figure out how. I have declared my 'collection' like this-
private static IMongoCollection<Project> SpeCollection = db.GetCollection<Project>("collection_Project");
And I followed what is explained in this MongoDB tutorial. I adjusted it for my needs, like-
var documents = await SpeCollection.Find(new Project()).ToListAsync();
However, I keep having the following error-
MongoDB.Driver.IMongoCollection does not have a definition for 'Find' and the best override of the extension method [superlong stuff]. Find contains non valid arguments.

Using the current version of the driver (v2.0) you can do that by passing a filter that matches everything:
var documents = await SpeCollection.Find(_ => true).ToListAsync();
They have also added an empty filter (FilterDefinition.Empty) which will arrive in the next version of the driver (v2.1):
var documents = await SpeCollection.Find(Builders<Project>.Filter.Empty).ToListAsync();

Simplest Way
Retrieve all the documents-
var documents = SpeCollection.AsQueryable();
Also convert to JSON object-
var json = Json(documents, JsonRequestBehavior.AllowGet);

If you want all documents, why not use Find all?
var documents = await SpeCollection.Find(new BsonDocument()).ToListAsync();

Related

What is the C# equivalent to Promise.all?

I would like to fetch data from multiple locations from
Firebase Realtime Database like described here and here by Frank van Puffelen and I can't find any equivalent to Promise.all in c#. What would be the proper way to do it?
That you are looking for is Task.WhenAll. You should create as many tasks as the multiple locations from which you want to fetch your data and then feed them in this method.
To expand on #Christos's accepted answer:
Task.WhenAll appears to be about as close as you will get for a drop-in replacement for Promise.all. I actually found it to be closer than I initially thought. Here's an example using a JavaScript Promise.all implementation that you may want to replicate in C#:
const [ resolvedPromiseOne, resolvedPromiseTwo ] = await Promise.all([ taskOne, taskTwo ]);
In C# you can do something very similar with Task.WhenAll (assuming they return the same types).
var taskList = new[]
{
SomeTask(),
AnotherTask()
};
var completedTasks = await Task.WhenAll(taskList);
// then access them like you would any array
var someTask = completedTasks[0];
var anotherTask = completedTasks[1];
// or just iterate over the array
foreach (var task in completedTasks)
{
doSomething(task);
}
This assumes they're both in async methods / functions.

Elasticsearch.Net and NEST, IGetResponse to document POCO?

I'm retrieving a document from Elasticsearch using the client.Get<MyDocument>(getRequest) syntax however the IGetResponse I retrieve is basically useless. It contains no fields of the document I want and basically only tells me that the .Get was successful (and includes the Id of the document I'm trying to get)
Here is my code:
TypeName typeName = TypeName.From<MyDocument>();
GetRequest request = new GetRequest(Index, typeName, new Id("R" + id));
// I can't get any of the fields I want from this object:
IGetResponse<MyDocument> result = Client.Get<MyDocument>(request);
My question is do I need to cast the IGetResponse<MyDocument> to a MyDocument somehow? Is there some step I'm missing here?
EDIT: P.S.: result.Found is true so it definitely succeeds in getting the document
Figured it out: the property on the IGetResponse<MyDocument> I want is Source. Its the actual document object.
e.g.:
IGetResponse<MyDocument> result = Client.Get<MyDocument>(request);
if (result.Found)
{
MyDocument myDocument = result.Source;
}
From the documentation:
The Get() call returns an IGetResponse that holds the requested document as well as other meta data returned from Elasticsearch.
response.Source holds the document.

Mongo C# driver - Contains Filter

I am using the latest version of Mongo C# driver which uses a lot of Async and builder pattern. Which is nice. I am trying to convert SQL where clauses into Mongo FilterDefinition object.
Any idea how to handle "contains"?
like:
where x contains 'ABC'
In order to achieve that in V2 API, use the `Filter.Regex':
var collection = db.GetCollection<BsonDocument>("collection");
var filter = Builders<BsonDocument>.Filter.Regex("fieldName", new BsonRegularExpression(".*fieldValue.*"));
var data = await (await coll.FindAsync<BsonDocument>(filter).ConfigureAwait(false)).ToListAsync();
//continue process data
If x is a string, you could do so with a simple regex. For the 2.0 driver, you can manually create the FilterDefinition:
FilterDefinition<BsonDocument> filter = "{ x : { $regex : /ABC/ } }";
Or build the filter use the Builder:
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Matches("x", "ABC");
Then you can use the filter in your query:
using (var cursor = await collection.Find(filter).ToCursorAsync())
{
// ...
}
I was able to get this working using Filter.AnyIn like so
var filter = Builders<BsonDocument>.Filter.AnyIn("x", new List<string> { "ABC" });
This works if you're looking for multiple values too, just add them to the list.
First, I highly recommend taking MongoDB University's .NET course (from Mongo itself). It's really thorough, and covers your question (and more) in depth.
Second, I assume that x is an array in your example.
MongoDB correctly handles polymorphism with arrays. If you have a class Post with an array of Tags, you can filter where Tag = ABC.
If you're using the C# linq methods, that looks like .Find(p => p.Tags == "ABC"). If you're using BsonDocument, that looks like new BsonDocument().Add("Tags", "ABC").
I have another way which I don't love but it works. The answer that is marked correct is half wrong (Matches is a method of Builders). In this example the / act like a % in a sql query LIKE statement. I'm still looking for a better way and will update if I find one that is more Equals filter below.
List<yourobject> someList = await collection.Find("{ x: /Test/ }").ToListAsync();
var filter = Builders<yourobject>.Filter.Eq("x", "ABC");
List<yourobject> someList = await collection.Find(filter).ToListAsync();
If you want to just search input text you need to replace regex special characters.
Regex.Escape will ensure that these characters are processed literally rather than as metacharacters. Otherwise input text can be used to query regex patterns which is probably not what is required.
var text = "ABC";
var filter = Builders<BsonDocument>.Filter.Regex("x", BsonRegularExpression.Create(Regex.Escape(text)));
If you need case insensitive check. Then you can pass case insensitive regex to BsonRegularExpression.Create:
var text = "ABC";
var escapeText = Regex.Escape(text);
var regex = new Regex(escapeText, RegexOptions.IgnoreCase);
var filter = Builders<BsonDocument>.Filter.Regex("x", BsonRegularExpression.Create(regex));

Search for one key in multi-level JSON array

I receive JSON output from several different web services. I need to obtain some token data from each service however it's in a different array each time. E.g.:
Service 1:
{
"service_name": "service1",
"service1_data": {
"token_data": "WSD123456789"
}
}
Service 2:
{
"service_name": "service2",
"service2_data": {
"token_data": "QSD76662345"
}
}
So I'm looking for a way to search for the value of "token_data" no matter where in the arrays it may be. At the moment I have to get it manually like so:
json1["service1_data"]["token_data"]
If theres a simple way to do this it would be much appreciated. Thanks!
You could convert the JSON to XML and then use an xpath like '//token_data' to find the token, assuming a simple string search or regex is not an option.
byte[] bytes = Encoding.ASCII.GetBytes(json);
using (var stream = new MemoryStream(bytes))
{
var quotas = new XmlDictionaryReaderQuotas();
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, quotas);
var xml = XDocument.Load(jsonReader);
}
Xpath info can be found here
As suggested in the comments, you could use JSONPath.
In your scenario, $.service1_data.token_data.* should get you all the values you want.
This works only if the depth of token_data in the array is always the same.
The Newtonsoft.Json package - available on NuGet Install-Package Newtonsoft.Json allows you to walk Json objects using LINQ expressions.
var jsonText = #"{
""service_name"": ""service1"",
""service1_data"": {
""token_data"": ""WSD123456789""
}
}";
var jsonObject = JsonConvert.DeserializeObject<JToken>(jsonText);
System.Diagnostics.Debug.Assert(jsonObject["service1_data"].Value<string>("token_data") == "WSD123456789");
See the JToken reference to learn how to retrieve values from Json.

Searching ElasticSearch using NEST C# Client

I started looking around for a search engine and after some reading I decided going with ElasticSearch (which is quite amazing :)), my project is in C# so I looked around for a client and started using NEST, everything is quite straightforward but I am a bit confused on the searching part.
I want to search all fields on a specific type what I came up with is the following code:
elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));
I saw that much of the string query search is deprecated and wanted to make sure the above is the correct way of doing this (the above is not marked as deprecated...) also it is a bit long for a simple task so maybe anyone knows another way of doing this.
Thanks
I just use the string query version: create my query object using C# anonymous type and serialize it to JSON.
That way, I can have straightforward mapping from all the JSON query examples out there, no need translating into this "query DSL".
Elasticsearch by itself evolves quite rapidly, and this query DSL thus is bound to lack some features.
Edit: Example:
var query = "blabla";
var q = new
{
query = new
{
text = new
{
_all= query
}
},
from = (page-1)*pageSize,
size=pageSize
};
var qJson = JsonConvert.SerializeObject(q);
var hits = _elasticClient.Search<SearchItem>(qJson);
Just to confirm
elasticClient.Search<NewType>(s => s.Query(q => q.QueryString(d => d.Query(queryString))));
Is the preferred way to search and the fact it feels a bit long is because there are alot of options you can play with that are not used here. I'm always open on suggestions to make it shorter!
The string overload is deprecated but wont be removed from NEST. I'll update the obsolete message to explicitly mention this.
If the anonymous types above aren't your thing, you can just use JObjects from json.net and build your query that way. Then you can run it the same way above.
JObject query = new JObject();
query["query"] = new JObject();
query["query"]["text"] = new JObject();
query["query"]["text"]["_all"] = searchTerm;
query["from"] = start;
query["size"] = maxResults;
string stringQuery = JsonConvert.SerializeObject(query);
var results = connectedClient.SearchRaw<SearchItem>(stringQuery);
I like this way better because the DSL in ES uses reserved keywords in C#, like bool, so I don't have to do any escaping.
With ElasticSearch 2.0, I have to use a SearchResponse< NewType > in the Search method like this :
var json = JsonConvert.SerializeObject(searchQuery);
var body = new PostData<object>(json);
var res = _elasticClient.Search<SearchResponse<NewType>>(body);
IEnumerable<NewType> result = res.Body.Hits.Select(h => h.Source).ToList();
Hope it help.
Note: I found very few documentation about PostData class and its generic type parameter

Categories