Proper way to access usersinfo using mongodb driver for .net/c# - c#

When I write this line of code (C# using mongodb driver):
var mongoDB = ...
var result = mongoDB.FindUser("ruprecht");
I get a warning that I should "Use the new user management command "usersinfo".
But I can find no example of how to do this.
I'll just use the deprecated commands for now, but I'd like to know the right way to do it.

I realize this thread is a couple years old, but this is a question I've been looking for an answer to for quite some time with great difficulty, so I figured I'd answer for the sake of future Googlers.
Your Code:
Your attempts were close, but not quite the syntax RunCommand is looking for. If you take a look at the Reference page (https://docs.mongodb.org/manual/reference/command/usersInfo/), the syntax for the Mongo Shell looks like this:
Mongo Shell Code:
{ usersInfo: { user: <name>, db: <db> },
showCredentials: <Boolean>,
showPrivileges: <Boolean>
}
When we translate that to C#, it's a bit more complex, but it'll look something like this.
C# Code:
command = new CommandDocument
{
{"usersInfo", new BsonDocument
{
{"user", "reprecht"},
{"db", "myDatabaseName"}
}
}
};
myOpenDb.RunCommand (command)
Note the new BsonDocument part in there. That's generally what you'll need to do in cases of nested brackets.
Another Example:
For the sake of completeness, let's look at another example say you wanted to add a new user. The documentation here shows it like this:
Mongo Shell Code:
{ createUser: "<name>",
pwd: "<cleartext password>",
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
writeConcern: { <write concern> }
}
So, ignoring the optional customData and writeConcern, we can write this in C# like this:
C# Code:
command = new CommandDocument
{
{"createUser", "myUser"},
{"pwd", "myPassword"},
{"roles", new BsonArray
{
new BsonDocument
{
{"role", "readWrite"},
{"db", "myDatabaseName"}
}
}
}
};
Again, note that we used new BsonArray to create an array like in the shell code.
Hopefully, that gives you a good place to start. You should be able to execute any command in this manner, though this is more of a guess/hope on my part than something I've extensively tested.

You use the MongoDatabase's RunCommand method and the usersInfo command documented here: http://docs.mongodb.org/manual/reference/command/usersInfo/#dbcmd.usersInfo.

Related

How to check if a tweet was sent with tweetinvi [duplicate]

I am relatively new to programming in C# (Learning on my own for a school project) and decided to try using TweetInvi to implement Twitter functionality.
So far it's going good, got the authentication and publishing up and running, but I'm struggling to find out how to use the DestroyTweet() method.
It, and many other methods takes a tweetID parameter, which I can't figure out of how to find for a specific tweet.
Using the following code to publish a tweet, how can i find the tweetID of this tweet?
public ITweet publishTweet(string text)
{
return Tweet.PublishTweet(text);
}
// Snippet from a test method in main class.
twitter.twitterUser.publishTweet(System.Console.ReadLine());
// Still working on GUI so using ReadLine for now.
It's probably an easy solution, but I just can't figure it out!
Thanks in advance.
You can try something like this:
public string PublishTweet(string text)
{
var appCredentials = new TwitterCredentials(_apiKey,_apiSecret, _accessToken, _accessTokenSecret);
Tweetinvi.Auth.SetCredentials(appCredentials);
text = "my tweet";
var publishedTweet = Tweetinvi.Tweet.PublishTweet(text);
var tweetId = publishedTweet.Id.ToString();
return tweetId;
}
You just need to get the published tweet into a var for the result of the PublishTweet() method then you select the field(s) you need.
Simple solution. As explained before you need to take the tweet back from PublishTweet.
string text = "text";
ITweet tweet = Tweet.PublishTweet(text);
bool destroySuccess = tweet.Destroy();

How to set MongoDB Change Stream 'OperationType' in the C# driver?

When running the new MongDB Server, version 3.6, and trying to add a Change Stream watch to a collection to get notifications of new inserts and updates of documents, I only receive notifications for updates, not for inserts.
This is the default way I have tried to add the watch:
IMongoDatabase mongoDatabase = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = mongoDatabase.GetCollection<BsonDocument>("TestCollection");
var changeStream = collection.Watch().ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
Then I downloaded the C# source code from MongoDB to see how they did this. Looking at their test code for change stream watches, they create a new document(Insert) and then change that document right away(Update) and THEN set up the Change Stream watch to receive an 'update' notification.
No example is given on how to watch for 'insert' notifications.
I have looked at the Java and NodeJS examples, both on MongoDB website and SO, which seems to be straight forward and defines a way to see both Inserts and Updates:
var changeStream = collection.watch({ '$match': { $or: [ { 'operationType': 'insert' }, { 'operationType': 'update' } ] } });
The API for the C# driver is vastly different, I would have assumed they would have kept the same API for C# as Java and NodeJS. I found no or very few examples for C# to do the same thing.
The closest I have come is with the following attempt but still fails and the documentation for the C# version is very limited (or I have not found the right location). Setup is as follows:
String json = "{ '$match': { 'operationType': { '$in': ['insert', 'update'] } } }";
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
PipelineDefinition<ChangeStreamDocument<BsonDocument>, ChangeStreamDocument<BsonDocument>> pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match(Builders<ChangeStreamDocument<BsonDocument>>.Filter.Text(json,"json"));
Then running the statement below throws an Exception:
{"Command aggregate failed: $match with $text is only allowed as the
first pipeline stage."}
No other Filter options has worked either, and I have not found a way to just enter the JSON as a string to set the 'operationType'.
var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
var next = changeStream.Current;
My only goal here is to be able to set the 'operationType' using the C# driver. Does anyone know what I am doing wrong or have tried this using the C# driver and had success?
After reading though a large number of webpages, with very little info on the C# version of the MongoDB driver, I am very stuck!
Any help would be much appreciated.
Here is a sample of code I've used to update the collection Watch to retrieve "events" other than just document updates.
IMongoDatabase sandboxDB = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = sandboxDB.GetCollection<BsonDocument>("TestCollection");
//Get the whole document instead of just the changed portion
ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
//The operationType can be one of the following: insert, update, replace, delete, invalidate
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{ operationType: { $in: [ 'replace', 'insert', 'update' ] } }");
var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext(); //Blocks until a document is replaced, inserted or updated in the TestCollection
ChangeStreamDocument<BsonDocument> next = changeStream.Current;
enumerator.Dispose();
The EmptyPiplineDefinition...Match() argument could also be:
"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"
If you wanted to use the $or command, or
"{ operationType: /^[^d]/ }"
to throw a little regex in there. This last one is saying, I want all operationTypes unless they start with the letter 'd'.

How to Use Google cloud Datastore with C#

I am new to google cloud datastore and trying to access 'KIND' and save to with C#. I have searched the documentation available on google cloud but did not find how to access data on google datastore and save to. I am able to authenticate.
There is proper documentation for JAVA, NODE.JS, GO and PYTHON but not for .NET.
Please help.
Yes, you can totally use C# to access google cloud datastore.
This sample shows you how:
https://cloud.google.com/dotnet/getting-started/using-cloud-datastore
Here's a direct link to the code:
https://github.com/GoogleCloudPlatform/getting-started-dotnet/blob/master/aspnet/2-structured-data/Models/DatastoreBookStore.cs
To search by Kind:
var query = new Query()
{
Limit = pageSize,
Kinds = new[] { new KindExpression() { Name = "Book" } },
};
To create a Key with the specified Kind:
public static Key ToKey(this long id)
{
return new Key()
{
Path = new KeyPathElement[]
{
new KeyPathElement() { Kind = "Book", Id = (id == 0 ? (long?)null : id) }
}
};
}
More documentation is coming soon.
I've wrote a mini .NET ORM for the Google Datastore:
https://github.com/NoGame/GoogleDatastoreORM
You might find it useful.
The actual API Reference is almost impossible to find. I eventually tracked it down here: https://googlecloudplatform.github.io/google-cloud-dotnet/docs/Google.Cloud.Datastore.V1/api/Google.Cloud.Datastore.V1.DatastoreDb.html#Google_Cloud_Datastore_V1_DatastoreDb_Create_System_String_System_String_Google_Cloud_Datastore_V1_DatastoreClient_
Hopefully this helps someone looking for all of the documentation.

C# Mongo RunCommandAsync

Is it possible to run
db.Users.createIndex({"FirstName" : 1, "LastName": 1});
from the RunCommandAsync also are there examples of what can be done from the command.
As a test I tried to run this but it through an exception
var indexCommand = new BsonDocumentCommand<BsonDocument>(new BsonDocument{
{"getIndexes", "Users"}
});
var results = await database.RunCommandAsync(indexCommand);
I know I can get and create indexes from the C# drivers, however I would like to keep a script in sync between those that want to build the indexes through C# and those that want to handle it directly on the DB.
You could probably try an alternative method. Especially if you see what RunCommandAsync is expected to do with the BsonDocument. See Unit test for RunCommandAsync
Assuming you are using mongodb 3.0, can you consider the code below an alternative?
using (var results = await context.Users.Indexes.ListAsync())
{
while (await results.MoveNextAsync())
{
foreach (var current in results.Current)
{
System.Diagnostics.Debug.WriteLine(current["name"].AsString);
}
}
}

How can I provide a tab completion list, with the values generated on powershell startup?

So I have a cmdlet written in c#: Get-LivingCharacter. I want users to use this like Get-LivingCharacter -Name "Bran", but I would like to allow for the list of available characters to change. Maybe today, "Bran" is a valid name to pass in for Get-LivingCharacter, but maybe in the future it will not be. Things happen.
For convenience I want to allow tab-completion of this field. However, I can't seem to get that to work for non-const data sets. Dynamic fields don't even auto-complete the field name, nevermind the value, and I don't know a way to implement this for a non-dynamic field. Conceptually, I could generate a .ps1 file on startup given the current data set, and then load that ps1 as the module, but this feels a bit like killing a pup with a greatsword - lots of overkill. Is there a better option?
I had already implemented a similar function to the DynamicParam helper function, as reference in the comments. However, tab completion wasn't working. I was writing a minimal reproduction example, when...my tab completion worked.
It turns out, it reproducibly works/breaks based on the inclusion of a WriteDebug statement:
[Cmdlet("Get", "LivingCharacter")]
public class GetLivingCharacter : Cmdlet, IDynamicParameters
{
protected override void ProcessRecord()
{
}
public object GetDynamicParameters()
{
WriteDebug("Getting names"); // Tab completion won't work with this here - comment it out and it works.
^^^^^^^^^^
var chars = new List<String>() { "Bran", "Arya" };
var dict = new RuntimeDefinedParameterDictionary();
var attributes = new Collection<Attribute>
{
new ParameterAttribute
{
HelpMessage = "Enter a valid open name",
Mandatory = true
},
new ValidateSetAttribute(chars.ToArray()),
};
dict.Add("Name", new RuntimeDefinedParameter("Name", typeof(string), attributes));
return dict;
}
}
After some digging, the WriteDebug statement is throwing (which I assume is because it can't output while I'm typing). It then recreates the GetLivingCharacter class after I've finished the command to validate. It took a while to find since, because of the issue, I can't write the error to the console, so I had to append to a temp file instead.

Categories

Resources