Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to run this mongodb query from C#:
db.Stories.find({_id: ObjectId("52318492c28f7c19d4270c7f")},
{Chapter:
{$elemMatch:{
ChapId:ObjectId("52318629c28f7c1ec4515000")
}}
})
Try to make something like this:
var server = MongoServer.Create("mongodb://localhost:27020");
var database = server.GetDatabase("someDb");
var collection = database.GetCollection<Type>("someCollection");
var searchQuery = Query.GT("myfield", 10).LT(20);
var list = collection.Find(searchQuery);
Reference: MongoDB C# Query expression (How to?)
What does Query.GT mean
My collection like this
And i want to get The chapcontent in a chapter by chapid
"AuthorName" : "From",
"CateName" : "biết chết liền",
"Chapter" : [
{
"ChapContent" : [
{
"ImgUrL" : "../Upload/Stories/523675fec28f7c04b088afd6/52367611c28f7c04b088afd7/12613_617411151612439_796025785_n.jpg",
"Page" : 1,
"PageId" : ObjectId("52367612c28f7c04b088afd8")
},
{
"ImgUrL" : "../Upload/Stories/523675fec28f7c04b088afd6/52367611c28f7c04b088afd7/21453_617411254945762_2771189_n.jpg",
"Page" : 1,
"PageId" : ObjectId("52367612c28f7c04b088afd9")
},
"DatePost" : ISODate("2013-09-16T03:07:42.096Z"),
"Description" : "dsngdsfhgdsjhfgds",
"FirstCharacter" : "b",
"LastModify" : ISODate("2013-09-16T03:07:42.096Z"),
"Status" : 1,
"StoryName" : "biết ai",
"Title" : "ai biêt",
"_id" : ObjectId("523675fec28f7c04b088afd6")
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I update "UserPhoneNumber" property value in "Data" property alone from this object. I have done only to change value of property and not nested object.
var updateProfile = generalSettingResponse.Select(x => settingsList.ContainsKey(x.Key) ? settingsList.First(y => y.Key.ToLower() == x.Key) : x);
sample json object:
{
"Name": "John",
"SNo": "1234",
"Data": {
"UserPhoneNumber": "9102287287",
"UserProfileName": "John"
}
}
If you want to change property of json object,here is a demo:
SampleJObject:
{ "Name": "John", "SNo": "1234", "Data": { "UserPhoneNumber": "9102287287", "UserProfileName": "John" } }
Use the following code,the property UserPhoneNumber will be changed:
SampleJObject["Data"]["UserPhoneNumber"] = "0123456789";
result:
You cannot use this:
updateProfile.Data.UserPhoneNumber = "+18888888888";
That will only work if the setters in the class are public.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to map the result of adds to Codes?
result.adds contains a string array:
string[] adds = new string[] { "A", "B", "C" };
Option 1:
var car = new Car()
{
Name = request.Name,
Codes = (result.adds != null) ? adds : new string[] { }
};
In my opinion option 1 seems strange with creating a new empty string array when the result is false. I also have my doubts about mapping within the object mapping itself. On the other hand, it's direct and fast.
Option 2:
if (result.adds != null)
{
adds = results.adds;
}
var car = new Car()
{
Name = request.Name,
Codes = adds
};
Option 2 null check seems overkill to define the code seperately. It could simply be included in the mapper.
Of the two options you've presented option #1 looks the cleanest. However, you can simplify it more if you can use the null coalescing operator:
var car = new Car()
{
Name = request.Name,
Codes = result.adds ?? Array.Empty<string>()
};
This will use an empty string array in results.add is null.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the following json string:
{
"data":
{
"id": "1",
"city": "London"
},
"cityDetails":
{
"_id": "1",
"location": "UK",
"th": 0,
"title": "Default Group",
}
},
"limit": 0.60451203584671021,
"_id": "1234"
}
How can I extract the the 'city' name from the 'data' section of the JSON string using Newtonsoft.Json in C#.
Try
// load your json here
var obj = JObject.Parse(#"{
""data"":
{
""id"": ""1"",
""city"": ""London""
},
""_id"": ""1234""
}");
// get the city
var city = (string)obj.SelectToken("data.city");
You need to update the selected-token path if the JSON you provided is part of/inside another.
Below is an example of my document. I am trying to update the CostReports part based on the id of the CostReportingPeriods element.
{
"_id" : "240106",
"CostReportingPeriods" : [
{
"FyBgnDt" : ISODate("2000-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2000-12-31T05:00:00.000Z"),
"_id" : "240106-20000101-20001231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2001-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2001-12-31T05:00:00.000Z"),
"_id" : "240106-20010101-20011231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2002-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2002-12-31T05:00:00.000Z"),
"_id" : "240106-20020101-20021231",
"CostReports" : []
},
{
"FyBgnDt" : ISODate("2003-01-01T05:00:00.000Z"),
"FyEndDt" : ISODate("2003-12-31T05:00:00.000Z"),
"_id" : "240106-20030101-20031231",
"CostReports" : []
}
]
I am using the following code to try and update that element but am getting an error that says cannot use the element (CostReportingPeriods of CostReportingPeriods.CostReports) to traverse the element. If I add CostReportingPeriods.0.CostReports it will add it to the first element of the array, regardless of the filter.
var builder = Builders<MongoModels.Provider>.Filter;
var filter = builder.Eq("_id",costReport.PRVDR_NUM) & builder.Eq("CostReportingPeriods._id", costReport.UNIQUE_ID);
var update = Builders<MongoModels.Provider>.Update.AddToSet("CostReportingPeriods.CostReports", Mapper.Map<CostReport, MongoModels.CostReport>(costReport, opt =>
{
opt.AfterMap((src, dest) => dest.Worksheets = CreateWorksheets(dest.RptRecNum).ToList());
}));
How do I get it to update the element I want it to update based on the id of the subdocument?
After trying a bunch of different things, by channging my update filter from "CostReportingPeriods.CostReports" to "CostReportingPeriods.$.CostReports" it works flawlessly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone explain theory this solution? And I have simple code
var PersonID = worksheet.Cells[row, 1].Value;//คอลัมน์ 1
var PersonName = worksheet.Cells[row, 2].Value;//int type in sql
var PersonAge = worksheet.Cells[row, 3].Value;//char type
var PersonCity = worksheet.Cells[row, 4].Value;//char type
var PersonBugged = worksheet.Cells [row,5].Value;// Float type
lsTestdbl.Add(new testdbl
{
id = PersonID == null ? "" : PersonID.ToString(),
name = PersonName == null ? "" : PersonName.ToString(),
age = PersonAge == null ? "" : PersonAge.ToString(),
city = PersonCity == null ? "" : PersonName.ToString(),
bugget = PersonBugged == null ?"" ; PersonBugged.ToString()
});
}
I already can import type char into SQL Server successfully, but the float and int I don't know how to do
You should convert(parse) strings to required types.
int num = string.IsNullOrEmpty(PersonID) ? 0 : int.Parse(PersonID);
double bugged = Convert.ToDouble(PersonBugged);