由于客户端代码中的错误,mongodb创建了许多"mr.mapreduce ...."集合,如何将它们全部删除(可能通过掩码).
我在我的测试项目中使用官方mongodb驱动程序用于c#,我已经将文件从c#web应用程序插入mongodb.在mongo控制台中,db.blog.find()可以显示我插入的条目.但是当我试图检索它们时,.net会抛出异常
"System.InvalidOperationException:只有在CurrentBsonType为String时才能调用ReadString,而不能在CurrentBsonType为ObjectId时调用."
我的实体类很简单
namespace MongoDBTest
{
public class Blog
{
public String _id
{
get;
set;
}
public String Title
{
get;
set;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的检索代码
public List<Blog> List()
{
MongoCollection collection = md.GetCollection<Blog>("blog");
MongoCursor<Blog> cursor = collection.FindAllAs<Blog>();
cursor.SetLimit(5);
return cursor.ToList();
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我吗?谢谢!
我是MongoDB的新手,我正在尝试让C#驱动程序工作序列化F#类.我使用可变F#字段和无参数构造函数使用类自动化程序,但实际上我需要保留不变性,所以我开始考虑实现IBsonSerializer来执行自定义序列化.我没有找到任何关于编写其中一个的文档,所以我们只是试图从驱动程序源代码中推断出来.
我遇到了一个问题,当在序列化程序上调用Deserialize方法时,CurrentBsonType被设置为EndOfDocument而不是我期待的开始.我在C#中编写了等效文件,以确保它不是一些F#怪异,但问题仍然存在.序列化部分似乎工作正常,可以从shell查询.以下是示例代码:
class Calendar {
public string Id { get; private set; }
public DateTime[] Holidays { get; private set; }
public Calendar(string id, DateTime[] holidays) {
Id = id;
Holidays = holidays;
}
}
class CalendarSerializer : BsonBaseSerializer {
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) {
var calendar = (Calendar) value;
bsonWriter.WriteStartDocument();
bsonWriter.WriteString("_id", calendar.Id);
bsonWriter.WriteName("holidays");
var ser = new ArraySerializer<DateTime>();
ser.Serialize(bsonWriter, typeof(DateTime[]), calendar.Holidays, null);
bsonWriter.WriteEndDocument();
}
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type …Run Code Online (Sandbox Code Playgroud) 我正在测试MongoDb的一些场景,以了解如何从可能的数据问题中恢复.
我有一个类(地址集合的地址)在地址中有一个zipcode属性,最初被转换为字符串.我保存了多个地址记录,可以很好地检索它们.像这样,var allAddresses = addresses.FindAllAs();
我将邮政编码属性更改为int并保存了一些记录.然后我将邮政编码属性更改回字符串.
当我尝试读取集合时,我得到了一个错误反序列化,正如预期的那样.var allAddresses = addresses.FindAllAs();
我的目标是能够覆盖反序列化,因此如果发生字段反序列化错误,我可以选择忽略它或应用默认值.
我尝试过自定义序列化程序,但它无法正常工作.任何建议,将不胜感激.
public class MyCustomSerializer : BsonBaseSerializer
{
public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
if (bsonReader.CurrentBsonType != BsonType.String)
{
return string.Empty;
}
return bsonReader.ReadString();
}
public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
IBsonSerializationOptions options)
{
bsonWriter.WriteStartDocument();
bsonWriter.WriteName("ZipCode");
bsonWriter.WriteString(value.ToString());
bsonWriter.WriteEndDocument();
}
}
Run Code Online (Sandbox Code Playgroud) 我是mongodb + C#驱动程序的新手,所以请原谅我的任何天真.
我正在尝试对一组键值对进行批量插入,因此我的数据结构是类型的List<Dictionary<string,string>>.
这是我的持久性代码示例:
public void Persist(string collectionName, List<Dictionary<string, string>> documents)
{
string connectionString = ConfigurationManager.ConnectionStrings[CONNECTION_STRING_KEY].ConnectionString;
MongoServer server = MongoServer.Create(connectionString);
MongoCredentials credentials = new MongoCredentials("MYUser", "MyPassword");
MongoDatabase myDb = server.GetDatabase("myDb", credentials);
var collection = myDb .GetCollection(collectionName);
using (server.RequestStart(myDb ))
{
var result = collection.InsertBatch(documents);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到有关序列化的错误:
MongoDB.Bson.BsonSerializationException:Serializer DictionarySerializer期望类型为DictionarySerializationOptions的序列化选项,而不是DocumentSerializationOptions.
我错过了设置吗?
编辑:更多信息
我的词典是我的实体.意思是,我只是将它们转储成一个,而不是创建一个保存属性的对象Dictionary.从mongo文档来看,它应该只是转换为mongo Document.
进一步编辑:扭曲问题
我可以通过将using语句更改为:来插入单个实例:
using (server.RequestStart(myDb))
{
foreach(var doc in documents)
collection.Insert(new BsonDocument(doc));
//var result = collection.InsertBatch(typeof(Dictionary<string, string>), documents);
}
Run Code Online (Sandbox Code Playgroud)
但是,我关注的是性能,因为在真实情况下我会轻松拥有10k +词典.使用此代码,驱动程序是否足够智能批量处理这些代码?有没有办法保持InsertBatch但完成同样的事情?
当然,任何帮助都非常感谢.
我正在使用ASP.NET MVC 4中的API,我使用MongoDB作为后端.
由于MongoDB存储和返回BSON对象,以及MVC4返回JSON对象,我认为在其余调用上简单地返回BSON会相当容易.
这不起作用,所以我.toJson()在BsonDocument类上找到了方法,将BSON对象转换为JSON字符串表示.不幸的是,当我通过my返回此字符串时ApiController,MVC显然认为它应该将字符串重新序列化为JSON,浏览器无法解释.
所以我想问一下是否有办法禁用特定ApiController方法的JSON序列化?
我目前的解决方法是在返回.toJson()之前对从返回的JSON 进行反序列化,使其再次序列化,但这似乎相当浪费.
我创建了一个这样的C#类:
public class Employee
{
[BsonRepresentation(BsonType.ObjectId)]
public string Name { get; set; }
public int Age { get; set; }
public List<string> Address { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试保存此信息(使用MongoDB)时,如下所示:
var e = new Employee();
e.Address = new List<string>();
e.Address.Add("Address 1");
e.Address.Add("Address 2");
e.Age = 333;
e.Name = "Some Name";
context.Employees.Insert(e);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
An unhandled exception of type 'System.FormatException' occurred in MongoDB.Bson.dll
Additional information: 'Some Name' is not a valid 24 digit hex string.
Run Code Online (Sandbox Code Playgroud)
如何使字符串字段ObjectID在MongoDB中起作用?
我正在使用C#驱动程序2.0测试MongoDB(服务器v 2.6.7).
当我使用插入函数存在InsertOneAsync一个_id存在的文档时,我期待一个像你从Mongo shell获得的错误:
Run Code Online (Sandbox Code Playgroud)WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.Commands.$_id_ dup key: { : 0.0 }" }})
但问题是带有C#驱动程序的插入不会抛出异常,我找不到WriteResult插入.当我查看数据库时,似乎什么都没发生.
所以我的问题是InsertOneAsync在插入现有内容时会发生_id什么?
Visual Studio中的代码:
IMongoCollection<BsonDocument> commandsCollection = db.GetCollection<BsonDocument>("Commands");
var bson = new BsonDocument
{
{"_id", i.Value},
{"label", i.Key}
};
commandsCollection.InsertOneAsync(bson);
Run Code Online (Sandbox Code Playgroud) 我有一个分层的类别文档,如父母 - 儿童 - 儿童等......
{
id: 1,
value: {
}Children: [{
id: 2,
value: {
}Children: [{
id: 3,
value: {
}Children: [{
id: 4,
value: {
}Children: [{
id: 5,
value: {
}Children: [{
id: 6,
value: {
}Children: [{
id: 7,
value: {
}Children: []
}]
}]
}]
}]
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
在这样的文档中,使用MongoDB C#驱动程序,我该如何找到一个节点 Id = x
我试过这样的事
var filter = Builders<Type>.Filter.Eq(x => x.Id, 3);
var node = mongoDbRepository.GetOne(filter) ??
mongoDbRepository.GetOne(Builders<Type>.Filter.Where(x => x.Children.Any(c=>c.Id == 3)));
Run Code Online (Sandbox Code Playgroud)
但这仅涵盖两个层面.在我的例子中,我有7个级别,我没有对级别深度的限制
一旦我找到该节点,我需要 …
我在我的项目中使用mongoDB c#最新驱动程序,即3. +.我使用daterangepicker有不同的日期过滤条件,如今天,最后一天,昨天,本月等.
这是我的模特
public class Student
{
public Student()
{
}
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime CreatedOn { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime ModifiedOn { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是驱动程序代码
var server = new MongoClient(_connectionString);
var db = server.GetDatabase("Students");
var collection = db.GetCollection<Student>("student");
var filterBuilder = Builders<Student>.Filter;
var start = new DateTime(2017, 03, …Run Code Online (Sandbox Code Playgroud)