[CSHARP-1086] An Array value cannot be written to the root level of a BSON document Created: 13/Oct/14  Updated: 05/Apr/19  Resolved: 13/Oct/14

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.0
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: Deepak Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: BsonDocument, SqlDataReader,, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2008 R2



 Description   

I am writing a small application.

I am querying data from SQL server using SqlDataReader object. I am using "ToBsonDocument" extension method on this object to create BsonDocument. While using this extension method i am getting above mentioned exception.

Could you please help me to solve this issue. Or let me know if i am doing anything wrong. Let me know what would be the best way to export data from SQL server to mongoDB so that i can be able to execute some queries in mongoDB similar to SqlServer.

Thanks in advance.



 Comments   
Comment by Craig Wilson [ 13/Oct/14 ]

The ToBsonDocument extension method is meant to be used on poco classes. It will not work on a SqlDataReader. You'll need to convert this manually or use AutoMapper.

Below is the simplest way to handle this.

while(reader.ReadNext())
{
  var doc = new BsonDocument();
  doc["firstName"] = (string)reader["firstName"];  
  // etc...
}

Generated at Wed Feb 07 21:38:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.