Not able to convert IMongoCollection<MatchDocument> to ObservableCollection<MatchDocument>
MongoClient client = new MongoClient();
var DB = client.GetDatabase("MTR");
var Collection = DB.GetCollection<MatchDocument>("MATCHES");
App.Profiles = new ObservableCollection<MatchDocument>(Collection);
You can use Collection.Find<MatchDocument>(query).ToListAsync():
MongoClient client = new MongoClient();
var DB = client.GetDatabase("MTR");
var Collection = DB.GetCollection<MatchDocument>("MATCHES");
Task<IList<MatchDocument>> doc = await Collection.Find<MatchDocument>(query).ToListAsync();
ObservableCollection<MatchDocument> Profiles = new ObservableCollection<MatchDocument>(doc.Result);
Refer: cannot convert from 'MongoDB.Driver.IMongoCollection<>' to 'System.Collections.Generic.IEnumerable<>'
Related
Please, give an easy example how to execute query. I found some examples on the net but they are not working.
Like
GraphqlClient client = new GraphqlClient("example.com\graphql");
String query = "query {getClient(condition){clientName, clientID}}"
var response = client.executeQuery(query);
This should work fine:
var graphQLOptions = new GraphQLHttpClientOptions
{
EndPoint = new Uri("example.com\graphql", UriKind.Absolute),
};
var graphQLClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());
var msg = new GraphQLRequest
{
Query = "query {getClient(condition){clientName, clientID}}"
};
var graphQLResponse = await graphQLClient.SendQueryAsync<dynamic>(msg).ConfigureAwait(false);
Basically here's my code which I'm having trouble with. Insanely new to mongoDB and would love to understand how to get values out of a JSON string that is returns in the variable 'line'.
public string get_data()
{
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("metacorp");
var cursor = collection.Find("{'movie_name' : 'Hemin'}").ToCursor();
var line = "";
foreach (var document in cursor.ToEnumerable())
{
using (var stringWriter = new StringWriter())
using (var jsonWriter = new JsonWriter(stringWriter))
{
var context = BsonSerializationContext.CreateRoot(jsonWriter);
collection.DocumentSerializer.Serialize(context, document);
line = stringWriter.ToString();
}
}
var js = new JavaScriptSerializer();
var d = js.Deserialize<dynamic>(line);
var a = d["movie_name"];
return line;
}
This is the output I get if I return line:
{ "_id" : ObjectId("58746dcafead398e4d7233f5"), "movie_name" : "Hemin"
}
I want to be able to fetch the value 'Hemin' into 'a'.
I know this is not what you're asking for but since you're using the c# driver then I would recommend the following. Assumes you have a c# class corresponding to metacorp collection or at least a serializer that handles it. Hope it helps.
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<MetaCorp>("metacorp");
var m = collection.SingleOrDefault(x => x.Movie_Name == "Hemin"); // Assuming 0 or 1 with that name. Use Where otherwise
var movieName = "Not found";
if(m!= null)
movieName = m.Movie_Name;
You could have your dto class for movie ans just fetch the data from collection:
public class Movie
{
public ObjectId Id { get; set; }
public string movie_name { get; set;}
}
...
var client = new MongoClient();
var db = client.GetDatabase("test");
var collection = db.GetCollection<BsonDocument>("metacorp");
var movies = collection.Find(x=>x.movie_name == "Hemin").ToEnumerable();
I am using paypal API for .NET to create payments.
My exact code in Console Application:
// Get a reference to the config
var config = ConfigManager.Instance.GetProperties();
// Use OAuthTokenCredential to request an access token from PayPal
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
var apiContext = new APIContext(accessToken);
var p = new Payment();
p.intent = "sale";
p.payer = new Payer();
p.payer.payment_method = "credit_card"; //paypal or credit_card
var t = new Transaction();
t.amount = new Amount();
t.amount.currency = "GBP";
t.amount.total = "10.00";
t.amount.details = new Details();
t.amount.details.subtotal = "6.00";
t.amount.details.tax = "2.00";
t.amount.details.shipping = "2.00";
t.item_list = new ItemList();
t.item_list.items = new List<Item>();
var i1 = new Item();
i1.quantity = "1";
i1.name = "OBJETO TESTE";
i1.price = "6.00";
i1.currency = "GBP";
i1.sku = "TESTE";
t.item_list.items.Add(i1);
var a = new ShippingAddress();
a.recipient_name = "ADDRESS";
a.line1 = "LINE1";
a.line2 = "LINE2";
a.city = "LONDOM";
a.country_code = "GB";
a.postal_code = "NW19EA";
t.item_list.shipping_address = a;
p.transactions = new List<Transaction>();
p.transactions.Add(t);
p.redirect_urls = new RedirectUrls();
p.redirect_urls.cancel_url = string.Format("{0}{1}", "http://localhost:3161/", "Order/CancelPayment");
p.redirect_urls.return_url = string.Format("{0}{1}", "http://localhost:3161/", "Order/CompletePayment");
var payment = Payment.Create(apiContext, p);
If I change payment method to paypal, its working. if I send credit_card I get error 500.
debug_id: 30e0f1bb08d3f
configuration: live
Merchants from UK cannot make Direct(Credit Card) Payments using REST API.
You will need to upgrade your account to PRO to make use of Direct Card Payments.
Only US merchants can make direct card payments in REST API without a PRO account.
I am trying to read a row from my MongoDB with the following code
public static void MongoConnection()
{
var connectionString = "127.0.0.1";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection("OCS.MeterEntity");
BsonDocument documentRead = mongodb.FindOne(new QueryDocument {
{"_id", "4B414D000000011613CD" }
});
But the documentRead is always null and i know the specifik _id exists in the database. What am i doing wrong?
It does connect to the database and the right table.
Seems to be a calling convention:
new QueryDocument( "_id", new ObjectId("4B414D000000011613CD") )
but this should work as well:
new BSONDocument { { "_id", "4B414D000000011613CD" } }
I mannaged to figure it out :)
public static void MongoConnection()
{
var connectionString = "mongodb://localhost";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection<MongoDBModel>("OCS.MeterEntity");
var mongodbQuery = Query<MongoDBModel>.EQ(x => x._id, "4B414D000000011613CD");
var foundMongoDB = mongodb.FindOne(mongodbQuery);
}
Hi I am trying to make a generic method to return a single value from the database
I am not sure where to put the return result, value is returned as a BSON document.I need 1 value from it....
public static string SearchMongoSingleValue(string key, string value, string ducument, string datatable)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase(datatable);
var collection = database.GetCollection(ducument);
var query = Query.EQ(key, value);
var oneDocument = collection.FindOne(query);
return oneDocument[value];
Thanks
I think you need oneDocument[key] and not oneDocument[value]. Just tested this code:
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
var client = new MongoClient("mongodb://localhost");
var coll = client.GetServer().GetDatabase("local").GetCollection("test1");
var doc = new BsonDocument();
doc.Add("Name","John");
doc.Add("Color","Red");
coll.Insert(doc);
var query = Query.EQ("Name", "John");
var doc2 = coll.FindOne(query);
var value = doc2["Color"];
It returns "Red" allright