I have the following method:
public async void generateTimeStampAsync()
{
FirestoreDb db = FirestoreDb.Create("MY Db project ID");
CollectionReference collection = db.Collection("users");
Query query = collection.WhereLessThan("Born", 1900);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
}
How would I use this or a similar method to be able to get a server timestamp from the database? Something similar to the ServerValue.TIMESTAMP in JavaScript. If you could provide any methods that is able to simply return the timestamp of the server to me, that would be great.
Related
I am trying to use an Azure CosmosDB (former DocumentDB) repository in an API I am creating (I'm quite new to C#). I have managed to get a result with all documents using the client.CreateDocumentQuery method without passing a SQL query to it. However, when I pass a SQL query, program execution hangs and the controller responds with 404. I've tried to add a try-catch around it but I get no exception or anything.
Using Microsoft.Azure.DocumentDB.Core Version 1.9.1.
I've tried many things, other methods and also LINQ, but got nothing to work. If you have a look at this sample method, could you give me an example on how to correctly query and get a result from the document collection? I would really appreciate it!UPDATE: I updated the SQL a bit. Have tried with hard coded parameter and have tested it in the data explorer where it works. Execution seems to freeze on *var feedResponse = await documentQuery.ExecuteNextAsync<JObject>();*
// Document repository
public async Task<IEnumerable<JObject>> GetDocsFromCollectionAsync(string someId)
{
DocumentClient client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
IDocumentQuery<JObject> documentQuery;
var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
var queryOptions = new FeedOptions { MaxItemCount = -1 };
if (someId.IsNullOrEmpty())
{
// This works
documentQuery = client.CreateDocumentQuery<JObject>(documentCollectionUri,queryOptions)
.AsDocumentQuery();
}
else
{
var query = new SqlQuerySpec(
"SELECT * FROM c WHERE c.someId = #someId",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "#someId", Value = someId } }));
// This hangs during execution / returns 404 in the API controller
documentQuery = client.CreateDocumentQuery<JObject>(documentCollectionUri, query, queryOptions)
.AsDocumentQuery();
}
List<JObject> documents = new List<JObject>();
while (documentQuery.HasMoreResults)
{
var feedResponse = await documentQuery.ExecuteNextAsync<JObject>();
documents.AddRange(feedResponse);
}
return documents; // Return documents to API controller
}
I have this code:
public async void SaveAuditLog(AuditLog a)
{
var db = new MongoDBContext();
var o = db.GetMongoDatabase(Common.Common.MongoDbConnectionString);
var audit = o.GetCollection<AuditLog>("AuditLog");
await audit.InsertOneAsync(a);
}
public IMongoDatabase GetMongoDatabase(string connectionstring)
{
MongoClient client = new MongoClient(connectionstring);
return client.GetDatabase("test");
}
this is the connection string from web.config:
<add connectionString="mongodb://localhost:27017" name="mongodb"></add>
when I check the data through robomongo, it does not show me any data inserted.
I have tried the following code as well and no data is inserted:
public async void SaveAuditLog(AuditLog a)
{
var client = new MongoClient(Common.Common.MongoDbConnectionString);
var o = client.GetDatabase("test");
var audit = o.GetCollection<BsonDocument>("AuditLog");
var document = new BsonDocument { {"Test", "test"} };
await audit.InsertOneAsync(document);
}
I am using csharpdriver for mongo with 2.2. What am I doing wrong?
found out that the data is getting inserted in mongodb and there is a bug in robomongo version 0.8.5 itself which does not show collections/documents for mongodb version 3 and above.
ran some scripts (in robomongo) which do return the data:
db.stats()
db.CollectionName.find()
downloaded mongochef and it displayed the data straight away.
I am trying something like
customer.Uid = Guid.NewGuid()
// set other properties
myCustomerService.Insert(myCompanyFile, myobCustomerContact, myCredentials );
myobCustomerContact = myCustomerService.Get(myCompanyFile, myobCustomerContact.UID,myCredentials);
The Get returns a 404 error.
What am I doing wrong?
You cannot currently control the UID used to insert a new entity; the API will create a new UID on insert for you.
When you use InsertAsync the task will return a string which is the full URI to the newly inserted entity. You can then retrieve the entity using
var location = await service.InsertAsync(cf, customer, credentials, ct);
var insertedEntity = await service.GetAsync(cf, new Uri(location), credentials, ct);
however if you want to insert and retrieve an inserted entity then you can use InsertExAsync e.g.
var insertEntity = await service.InsertExAsync(cf, customer, credentials, ct);
How to connect couchDB with ASP.NET C# application? If any one can you give a sample application.
I had the same need and after evaluating the options available, to meet the requirements of my application, I created any components that helped me a lot and maybe they can help you and also others. I make it clear that I have no intention of promoting myself here, just sharing something that may be useful.
The detailed explanation of how to configure and use it is on Github.
Link: Nuget Package |
Github
Example of use for retrieving documents with mango-querie:
IList<User> users;
var sts = new List<String> { "ACTIVE", "LOCKED" };
using (UserRepository db = new UserRepository())
{
var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts });
users = db.List<User>(query);
}
Array.ForEach(users.ToArray(), Console.WriteLine);
Example of adding documents:
User user = createUser("email#email.com");
using (UserRepository db = new UserRepository())
{
var result = db.Insert<User>(user); // add document and return instance changed with operation revision id
Console.WriteLine(result.Revision);
}
Example of changing documents:
using (UserRepository db = new UserRepository())
{
// Load document data by ID
var user = db.Get<User>("email#email.com");
user.Name = user.Name + "::CHANGED";
var result = db.Update<User>(user); // update document and return instance changed with operation revision id
Console.WriteLine(result.Revision);
}
Example of deleting documents:
using (UserRepository db = new UserRepository())
{
// Load document data by ID
var user = db.Get<User>("email#email.com");
var result = db.Delete<User>(user); // delete document from database. Return true case sucess or false case not deleted
Console.WriteLine($"Sucesso: {result}");
}
After installing the NuGet, just create an instance of MyCouch.Client and pass it the URL of your database.
using (var client = new MyCouchClient("http://127.0.0.1:5984/test"))
{
//Consume here
}
The format is: {scheme}://[{username}:{password}]/{authority}/{localpath}. From v0.11.0, there's a specific MyCouchUriBuilder that you can use for building the Uri. It will automatically e.g. apply Uri.EscapeDataString to username and password when calling SetBasicCredentials.
var uriBuilder = new MyCouchUriBuilder("http://localhost:5984/")
.SetDbName(TestConstants.TestDbName)
.SetBasicCredentials("foob#r", "p#ssword");
return new MyCouchClient(uriBuilder.Build());
For more details Click Here
I am trying to make a project where Parse.com is storing my data.
I have two classes:
User and
UserData.
When running my code it uses the objectID from UserData, however I would like it to get the object from UserData by using the username of the current user, which I can fetch with this code:
var curUser = ParseUser.CurrentUser.Username.ToString ();
My UserData class contains the a random ObjectID, username which is simlar to the one in the User class and also the data rows.
My code currently works with my single user as I have specified the user objectID in the code right now ("dpLevcJwVP") , but I don't know how to get any else users data as said before, by using the currents users username.
My code so far which gets the data:
public async void Data()
{
ParseQuery<ParseObject> query = ParseObject.GetQuery("UserData");
ParseObject userData = await query.GetAsync ("dpLevcJwVP");
var dataString = userData.Get<string> ("myDataRow");
Console.WriteLine ("Data fetched: " + dataString);
}
Problem solved.
Used the following code:
public async void Data()
{
var curUser = ParseUser.CurrentUser.Username.ToString ();
Console.WriteLine ("User: " + curUser);
var query = ParseObject.GetQuery("UserData")
.WhereEqualTo("username", curUser);
IEnumerable<ParseObject> results = await query.FindAsync();
ParseObject userData = await query.FirstAsync ();
}