How to make a list of big strings? - c#

What should I use to create a list of big strings?List<string> myList=new List<string>();
myList.Add("very big string goes here");
Note that this way is giving an overflow error in csharp. What should I use instead of it?
Example string {
"_id" : ObjectId("59e625d5fdde4f78f8b3df28"),
"ID" : ObjectId("000000000000000000000000"),
"UserID" : "abcdef",
"BaseFileName" : "insert",
"FileID" : "insert.00001.txt.txt",
"FileData" : "��/F\u000f"
}

Related

MongoDB - Update or Insert object in array in C#

I have the following collection
{
"_id" : ObjectId("57315ba4846dd82425ca2408"),
"myarray" : [
{
userId : ObjectId("570ca5e48dbe673802c2d035"),
point : 5
},
{
userId : ObjectId("613ca5e48dbe673802c2d521"),
point : 2
},
]}
and I want to add a new element to this array in C#, Could please to help me I try to do it's by $push but always give me this error. thanks
MongoDB.Driver.AddToSetUpdateDefinition<TDocument, TItem>.Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)`enter code here`

JSON parsing using c# and LitJson

I have a JSON data (file attached). How can I parse it using C# using LitJSON? I am having trouble in deserialising it. Any help would be appreciated.
{
"vr-sessions" : {
"-KvDNAFD_BxlKEJ958eX" : {
"interview" : "Android",
"questions" : {
"-KvG7BuWu4eI52pq-6uH" : {
"playaudio" : "audio1",
"question" : "Why cannot you run standard Java bytecode on Android?"
},
"-KvG9rxQv5DWJa1EMnhi" : {
"playaudio" : "audio2",
"question" : "Where will you declare your activity so the system can access it?"
}
}
},
"-KvDNOE8YhVdgovxrzrN" : {
"interview" : "DevOps",
"questions" : {
"-KvMPd0v9BXnjYZxFm5Q" : {
"playaudio" : "audio3",
"question" : "Explain what is DevOps?"
},
"-KvMPi24OKeQTp8aJI0x" : {
"playaudio" : "audio4",
"question" : "What are the core operations of DevOps with application development and with infrastructure?"
},
"-KvMPqYxJunKp2ByLZKO" : {
"playaudio" : "audio5",
"question" : "Explain how “Infrastructure of code” is processed or executed in AWS?"
}
}
},
After adding a couple of missing braces and removing last comma, your json seems to be valid. However there's no way to declare a class or member starting with dash in C#. Not sure how LitJson would be able to map json values to a C# class if names don't match, unless you replace dashes before parsing the string with LitJson.
Workaround
In your Solution Explorer right click References and do Add Reference, then from Assemblies->Framework select System.Web.Extensions.
string jsonString = File.ReadAllText("json.txt");
dynamic json = new JavaScriptSerializer().Deserialize<dynamic>(jsonString);
Navigate to the value you're looking for
string interview = json["vr-sessions"]["-KvDNAFD_BxlKEJ958eX"]["interview"];
Variable interview gets value "Android" which is the same as doing this:
var sessions = json["vr-sessions"];
string interview = sessions["-KvDNAFD_BxlKEJ958eX"]["interview"];
If you don't know session names
Iterate to get question and playaudio values.
foreach (var session in json["vr-sessions"].Values)
{
foreach (var question in session["questions"].Values)
{
Console.WriteLine(question["question"]);
Console.WriteLine(question["playaudio"]);
}
}
Access to an element by position: let's say you want to iterate 0..N
var sessions = new List<dynamic>(json["vr-sessions"].Values);
Console.WriteLine(sessions[0]["interview"]);
This prints "Android" as the value of interview for session at position 0 is "Android".

Mongodb: How can I check if a point is contained in a polygon?

I've got a list of points of a area in an array of points (latitude, longitude). I 've made an index on these arrays and now I want to know if one point is inside that polygon.
Is it possible with MongoDB?
I already tried with these commands but no luck:
> polygonA = [ [ 48.780809,2.307129],[ 48.781809,2.300129],[ 48.770809,2.317129]]
> db.contours.find({ "rings.ring" : { "$within" : { "$polygon" : polygonA } } })
and
> db.runCommand( { geoNear : "contours" , within : [2.307129,48.780809,], num : 10 } );
My data structure is:
> db.contours.findOne({},{'rings':0})
{
"_id" : ObjectId("50364617d591ac166000c196"),
"foundfieldname" : "Name",
"geometrytype" : "geometryPolygon",
"attributes" : {
"Shape" : "Polygon",
"Name" : "France",
"Type" : "Country",
"Country" : "France",
"Area" : "1162358716567.45"
},
"country" : "France",
"rings":{
"ring":[[12.32,43.54],...],
...
}
Thanks
This requires that rings.ring be a points and that it have a geo index defined on it, is that the case here?
Your question implies that this is in fact a list of multiple points with a standard index on it (multikey index), which is not going to work.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing/#GeospatialIndexing-BoundsQueries
As you can see there, when you search for "loc" as a point inside a polygon, the "loc" field is something like this (see the link above for other valid examples):
{ loc : [ 50 , 30 ] }
With an index something like this:
db.places.ensureIndex( { loc : "2d" }
That is, a field representing a single point and with a geo index defined on it. If you use a field like that - does your testing then work?

Mongodb Array ElemMatch

I have a collection of documents:
"_id" : ObjectId("500d1aa9cf6640c15214fc30"),
"Title" : "Title0",
"Description" : "Description0",
"Keywords" : ["Keyword000", "Keyword001", "Keyword002", "Keyword003", "Keyword004", "Keyword005", "Keyword006", "Keyword007", "Keyword008", "Keyword009"],
"Category" : 0
I would like to query for items that have one keyword:
var query = Query.ElemMatch("Keywords", Query.EQ(XXX, "Keyword003"));
I have no idea on what to query on Query.EQ.
By turning the example into:
"_id" : ObjectId("500d4393cf6640c152152354"),
"Title" : "Title0",
"Description" : "Description0",
"Keywords" : [{
"Value" : "Keyword000"
}, {
"Value" : "Keyword001"
}],
"Category" : 0
And querying by
var query = Query.ElemMatch("Keywords", Query.EQ("Value", "Keyword001"));
I have no problem on getting the results.
Thank you.
The MongoDB query engine treats queries of the form { x : 123 } differently when x is an array. It matches any document where the x array contains 123. See:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray
In your case, the query:
Query.EQ("Keywords", "Keyword003")
will match any document where the Keywords array contains "Keyword003". It might also contain other values, but that doesn't matter.
ElemMatch is only needed when the array is an array of embedded documents and you want to write a complex test against each of the embedded documents.
Have you tried quering keywords like the other keys in your document? The driver will returns the documents that contain the provided keyword
Bye

Insert in to a nested array Using C# Official Driver MongoDB

Here is my exact schema:
{
"_id" : ObjectId("4fb4fd04b748611ca8da0d48"),
"Name" : "Categories",
"categories" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d46"),
"name" : "Naming_Conventions",
"sub-categories" : [{
"_id" : ObjectId("4fb4fd04b748611ca8da0d47"),
"name" : "Namespace_Naming",
"standards" : []
}]
}]
}
As you can see I have an array named "standards" nested way down in there. How would I programmatically insert in to that using the C# driver? I have tried all of the examples I have found online but none of them are working.
Something like the below. Obviously, if any of these are not present on the way down to it, you're going to get a null reference exception.
var doc = collection.FindOne(Query.EQ("_id", new ObjectId("4fb4fd04b748611ca8da0d48")));
var standards = doc["categories"]
.AsBsonArray[0]
.AsBsonDocument["sub-categories"]
.AsBsonArray;
standards.Add(new BsonDocument());
collection.Save(doc);

Categories

Resources