0
我的數據庫中的文檔是使用node.js創建的。 https://github.com/mongodb/node-mongodb-native我正在使用mongodb c#驅動程序,當我嘗試訪問它們時出現錯誤。我猜想,node.js驅動程序創建的目標與c#驅動程序創建的目標不同。在node.js中創建文檔時使用c#驅動程序
Unhandled Exception: System.IO.FileFormatException: An error occurred while deserializing the _id property of class trader.Formula: 'CD2j7m4qDLMowGCXi' is not a valid 24 digit hex string. ---> System.FormatException: 'CD2j7m4qDLMowGCXi' is not a valid 24 digit hex string.
at MongoDB.Bson.ObjectId.Parse(String s)
at MongoDB.Bson.Serialization.Serializers.ObjectIdSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializati onOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
--- End of inner exception stack trace ---
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions)
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message)
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst()
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext()
我該如何解決這個錯誤?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace trader
{
public class Formula
{
public ObjectId _id { get; set; }
public string name { get; set; }
}
class Program
{
static void Main(string[] args)
{
MongoClient client = new MongoClient("mongodb://localhost:27017");
MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase("trader");
var formulaCollection = db.GetCollection<Formula>("formulas");
var formulas = formulaCollection.FindAll();
foreach (var formula in formulas)
{
Console.WriteLine(formula.name);
}
}
}
}
這是db.formulas.find({},{名稱:1})
{ "_id" : "9RZuitaJ7uKvrBKbE", "name" : "10d sma" }
{ "_id" : "kjLzXChio8wD2MQcD", "name" : "< 10d mean" }
{ "_id" : "mmjSnHS5L2GztXPLX", "name" : "< 2h mean" }
{ "_id" : "bH5zBEz5CnMFJMnSZ", "name" : "< 2h mean > 0.4 sd2h" }
{ "_id" : "A2joSNeBxPZTJgkGs", "name" : "< 2d mean > 0.4 sd2d" }
{ "_id" : "6jgQvFci3mW5Aijr6", "name" : "< 2d mean > 0.4 sd2d 2h sma" }
{ "_id" : "hHBRtu2nWZiYBdHGq", "name" : "2d sma" }
我編輯了一些結果的問題。它看起來像是字符串,我想我只是意識到了爲什麼。我也在使用Meteor.js,這些公式是在meteor.js中創建的,我記得它讀到了與objectids有關的一些問題。有沒有辦法讓這些工作在C#中?謝謝您的幫助! – Dan
是的,你說得對,他們是字符串。我將嘗試將它們全部轉換爲objectIds。 – Dan