How to query field equals empty array? - c#

I have an array field in my collection which have value with properties or empty value as "[]"
For fetching the documents having empty "[]" value in mongodb i use
db.getCollection('collection').find({"ArrayFieldName":{$eq:[]}})
This gives me result as expected. But when i try to form this query in the C# using mongodb driver i couldnt get the expected Result.
I tried,
filterBuilder.Eq("ArraryFieldName", "[]") and filterBuilder.Eq("ArraryFieldName", new ArraryClassName(){})
Need help with C# filter builder to specify $eq:[] for arrary field.

An instance of ArraryClassName clearly won't work because it's not an array instance - it's a single object. Likewise "[]" won't work because it's a string.
You can check directly translate your CLI query to this filter:
Builders<BsonDocument>.Filter.Eq<BsonArray>("ArraryFieldName", new BsonArray())
Though if you simply want to check that the existing array is empty, you can use this filter instead:
Builders<BsonDocument>.Filter.Size("ArraryFieldName", 0)
P.S. I would strongly suggest using C# data models as it makes everything much easier to work with.
Also, you have called your field ArraryFieldName (notice the extra r at the end of Array). If you don't have existing data with this misspelled property name, you might want to correct the spelling.

Related

CosmosDB SQL API: Change property Name - (Partial Updates .Net SDK or loops)

Consider the following data
I am trying to update the property name from givenName to PetName. Below are the things I tried.
Option 1:
List<PatchOperation> patchOperations = new List<PatchOperation>();
dynamic data = new
{
PetName= "Dummy"
};
//Remove the 0th element from the pet array
patchOperations.Add(PatchOperation.Remove("/children/0/pets/0"));
//insert the new entity
patchOperations.Add(PatchOperation.Add<dynamic>("/children/0/pets/0", data));
await Container.PatchItemAsync<dynamic>("AndersenFamily", new
PartitionKey("AndersenFamily"), patchOperations);
This works on the first element (as I have specified the index as 0).
But I couldn't find a way to update all the elements in the array. Something like /children/*/pets (I get an error "Invalid navigation for array(*)").
Option 2:
Read all the data from the database, remove the existing pets and upload the new object with the property PetName. But I guess it will take lot of time to loop through all the objects and make these changes.
Can someone suggest a better solution or a way to fix the partial updates so that it updates all the elements in the array?
The actual dataset that I am working on is huge and has more nested arrays.
Yes you are right, Current version of Patch does not support patching nested array. You need to know the index in order to replace/add values as you mentioned in the first question.
One way to think about this would be to write your own logic to replace the keys , there are libraries available in dotnet and then using the bulk insert using the SDK to insert those documents.

How can I check if date falls between two dates in mongoDB using C#?

I have a list stored in MongoDB that contains objects. Each object has a start date and an end date.
When I insert a new object into a list, I check whether it exists in the collection by using his ID.
If the object exists, I want to check that the dates do not overlap...how do i check it?
Thanks
I'm not sure what you have tried so far but I don't want to outright give you all the code especially since I don't know what you have done.
The.find() query will return all documents a collection and returns all fields from the documents. The documentation is here In addition to this I would probably use the $exists keyword within MongoDB in order to test the collection.
After the .find query finds the dates and you filter the query then we can use the $lte and $gte to filter through the dates and get them in a certain sequence so we can see which dates are between which range. The $lte and $gte syntax words are hyperlinked for you to check out the documentation.
If this doesn't help I also think this could be a duplicate of the following question:MongoDB_Possible_Duplicate

DataView.ToTable not returning distinct values

I'm attempting to utilize the DataView.ToValue function, and while it is pulling the correct columns needed, I am still not getting the DISTINCT values. I have a DataValue that contains a gridview of all of the information on the website. When a client goes to download this data in a specific format, they need just the unique values to be processed into the file.
At present, I store all of the columns needed for the report within an array with the purpose of being dynamic. Once that string array has been created, I create my DataTable with
tblData = dvData.ToTable(true, arrColumns);
Despite this, the data is still coming back with all rows. Am I missing something? According to this from Microsoft's documentation I SHOULD be getting back distinct values.
Turns out I forgot that a step on the backend handles cleaning up NULLs into valid datapoints for the xml. So it was properly sorting, just the null fields were actually causing "copies".

Lucene empty query string with filter

I am using Lucene.Net in a personal project and need to handle to cases but can't find a nice way that lucene will handle the two cases using the same type of query.
The basic query uses a MultiFieldQueryParser with the StandardAnalyzer and a NumericRangedFilter to filter by date (dates are saved as long values).
The problem being that I would like the filter to handle an empty search string, without having to use two different query parsers, one for an empty search string and one where the user enters a search string. Currently the MultiFieldQueryParser throws a ParseException when an empty string is used.
Any advice on the best way to handle this? Or is this a flaw (intentional or otherwise) in Lucene or Lucene.Net.
RESULT
I ended up using the MatchAllDocsQuery if the query string was empty with a normal query otherwise.
Also I had to remove the use of NumericFields and the NumericRangeFilter as the query returned no results when I used them. I ended up doing the date range filter the old way with strings and a normal RangeFilter.
The best way to handle it is to generate a MatchAllDocsQuery and bypass the parser if the input is an empty string.
http://lucene.apache.org/core/old_versioned_docs/versions/2_9_4/api/all/org/apache/lucene/search/MatchAllDocsQuery.html

getting a string value from object

Hello I have an object which has value "B11" i.e
object data = "B11";
i have a variable called string resultCell. How do I get B11 into resultCell.
it doesn't seem to work for me in c#. I have tried things like ....
resultCell = (string)data;
resultCell = Convert.ToString(data);
More information
I am using excel services to get a value in a cell. the excel services returns a object[]. Its a single element though. I want the value in a string. i.e. B11 in the object as string.
Much of the time, calling data.ToString() will work. Without the specific type of the data object, there isn't any way to answer the question.
Is the value stored in a property of data?
Edit: Could you try (string)object[i], where i is the index of the particular piece of data you want to retrieve? If you need every value in the array, you'll probably want to use a foreach loop.
i think you need to just assign value as data.ToString() to cell.
hope this work for you.

Categories