C#MVC ASP.netアプリケーションからFindOneを実行しようとしています。私はMongoDBのシェルを起動し、次のコマンドを実行した場合コマンドの検索に失敗しました:<dbname>でコマンドを実行できません{find:<コレクション名>
An unhandled exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll
Additional information: Command find failed: not authorized on twitterstream to execute command { find: "tweets", filter: {}, limit: 1, singleBatch: true }.
:
> use twitterstream
switched to db twitterstream
> db.auth("demouser", "abcd")
1
> db.tweets.findOne()
findOne()コマンドが実行され、レコードを表示する
残念ながら、これは次のエラーで失敗します。
私は間違っていますか、私のC#アプリケーションの中で何が間違っていますか?
私は以下の通りであるコンソールアプリケーションを使用してこの問題を再現している可能な限りテストケースは限り小さくするには:
私はMongoDBの3.4.6を使用しています。
コンソールアプリケーションは、2つのNuGetパッケージに依存しています。 MongoDB.Driver(2.4.4)とmongocsharpdriver(2.4.4)
App.configファイル
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<appSettings>
<add key="MongoDatabaseName" value="twitterstream" />
<add key="MongoUsername" value="demouser" />
<add key="MongoPassword" value="abcd" />
<add key="MongoPort" value="27017" />
<add key="MongoHost" value="localhost" />
</appSettings>
</configuration>
MongoContext.cs
using System;
using System.Configuration;
using MongoDB.Driver;
namespace DisplayingMongoConsoleApp
{
public class MongoContext
{
MongoClient _client;
MongoServer _server;
public MongoDatabase _database;
public MongoContext()
{
// reading creditials from web.config file
var MongoDatabaseName = ConfigurationManager.AppSettings["MongoDatabaseName"];
var MongoUsername = ConfigurationManager.AppSettings["MongoUsername"];
var MongoPassword = ConfigurationManager.AppSettings["MongoPassword"];
var MongoPort = ConfigurationManager.AppSettings["MongoPort"];
var MongoHost = ConfigurationManager.AppSettings["MongoHost"];
// creating creditials
var credential = MongoCredential.CreateMongoCRCredential(MongoDatabaseName, MongoUsername, MongoPassword);
// creating MongoClientSettings
var settings = new MongoClientSettings
{
Credentials = new[] { credential },
Server = new MongoServerAddress(MongoHost, Convert.ToInt32(MongoPort))
};
_client = new MongoClient(settings);
_server = _client.GetServer();
_database = _server.GetDatabase(MongoDatabaseName);
}
}
}
TweetModel
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace DisplayingMongoConsoleApp
{
public class TweetModel
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("thetweet")]
public string Tweet { get; set; }
}
}
の
Program.csの
namespace DisplayingMongoConsoleApp
{
class Program
{
static void Main(string[] args)
{
MongoContext mongo = new MongoContext();
var tweetDetails = mongo._database.GetCollection<TweetModel>("tweets").FindOne();
}
}
}
更新1:ここではありdemouserの役割は以下のとおりです。
> db.getUser("demouser")
{
"_id" : "twitterstream.demouser",
"user" : "demouser",
"db" : "twitterstream",
"roles" : [
{
"role" : "readWrite",
"db" : "twitterstream"
},
{
"role" : "read",
"db" : "twitterstream"
}
]
}
@Mate:更新中のdemouserにMongoの役割を追加しました –
MongoDB.Driver(2.4.4) – Mate