[CDRIVER-142] inserted 'undefined' field what I didn't insert while object inserting Created: 25/May/12  Updated: 11/Sep/19  Resolved: 25/May/12

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 0.4
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Seunghoon Park Assignee: Unassigned
Resolution: Done Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows, windows 7, 64bit



 Description   

hi, I'm using this library to connect to MongoDb in my project.
I found strange thing when I insert some object into MongoDb.
following is my test code and result.

bson bs;
bson_init(&bs);
bson_append_start_object(&bs, "ObjName");
bson_append_start_array(&bs, "ArrName");
bson_append_start_object(&bs, "1");
bson_append_string(&bs, "str1", "val1");
bson_append_finish_object(&bs);
bson_append_start_object(&bs, "2");
bson_append_string(&bs, "str2", "val2");
bson_append_finish_object(&bs);
bson_append_start_object(&bs, "3");
bson_append_string(&bs, "str3", "val3");
bson_append_finish_object(&bs);
bson_append_finish_array(&bs);
bson_append_finish_object(&bs);
bson_finish(&bs);
bson_bool_t res = mongo_insert(
conn,
"MyTestDb.Test1",
&bs
);

after success to insert, when I check that data in the mongo shell,
they returns like following.

show dbs;
MyTestDb 0.078125GB
local (empty)

use MyTestDb
switched to db MyTestDb
show collections;
Test1
system.indexes

db.Test1.findOne();
{
"_id" : ObjectId("4fbee85a1a374e84411c7eb4"),
"ObjName" : {
"ArrName" : [
undefined,
{
"str1" : "val1"
},
{
"str2" : "val2"
},
{
"str3" : "val3"
}
]
}
}
there is 'undefined' field what I didn't insert.
if this is already known issue, please let me know.
I'll glad to receive any links, reply about this issue.

thanks!

-surym



 Comments   
Comment by Kyle Banker [ 25/May/12 ]

Yes.

Comment by Seunghoon Park [ 25/May/12 ]

oh, thanks for comments.
actually, It's hard to find comments like you in driver header file.
I just give the name for object to insert more than one object into array,
however,
according to your comments,
I have to give the index as the name of the object in the array.
am I right?

Comment by Kyle Banker [ 25/May/12 ]

The issue is that arrays are zero-indexed. So when you call bson_append_start_object(), you must start with "0", not "1" as you've done in your example.

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