count always zero

100 views
Skip to first unread message

Martinus Martinus

unread,
Aug 30, 2012, 6:19:30 AM8/30/12
to mongod...@googlegroups.com
Hi,

I print my method result in java before I insert my data into the collection, but when I see the collection it's empty. Anyone can tell me what can cause this?

Thanks.

Adam C

unread,
Aug 30, 2012, 6:34:30 AM8/30/12
to mongod...@googlegroups.com
Can you be a bit more specific, perhaps give a sample of the code/result?  Not sure what exactly is failing here based on the description....

Thanks,

Adam

Martinus Martinus

unread,
Aug 30, 2012, 7:14:22 AM8/30/12
to mongod...@googlegroups.com
Hi Adam,

I tried to insert a long document inside a collection in empty database using java driver. After I finished running my java application, I can see the collection name inside my DB, but I the quantity is 0. I tried many times and it's always the same result.

{
"_id": 1,
"date": ISO Date ,
"species": "dog",
"name": "Dingo",
"food": "meat",
"birthday": ISO Date,
"meals": [{
"desc":"meat",
}],
"house_numbers":[{
"_id": 2,
"date": ISO Date ,
"trainer": "Ken",
"training_types": ["jumping", "sitting"],
"state": "Still learning",
"progress": 0.7,
}]
"skills":{
"jumping": 0.3,
"sitting": 0.4,
                        "smiling" : 0.0,
                        "fetching" : 0.0
}
"friends":[{
"_id": 3,
                        "name" : "Dig",
"cage_num": 7,
"status": "good friend",
}]
"behaviors": [{
"date": ISO Date,
"aggressiveness_level" : 0.2,
"reactions" : {
"water" : "playful",
"wind" : "open mouth",
"tennis_ball" : "wiggle tail"
                }
"sleep_time": "night",
}],
"diseases":{ 
"inner" : {
"stomach" : "none",
"throat" : "none",
"teeth" : "none",
"eyes" : "none",
                        "bones" : "none"
}
                "outer" : {
                        "skin" : "none",
                        "nails" : "none",
                        "fur" : "none"
                }
},
"owner" : [{
"_id": 5,
"complaint": "none",
"job": "grease monkey",
"skills_wanted": {
"sitting" : "good",
"fetching" : "good",
"jumping" : "good",
}
}]
}

Thanks.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Jenna

unread,
Aug 30, 2012, 10:47:03 AM8/30/12
to mongodb-user
Can you show us the command that you used to insert the document? How
are you determining that the collection is empty?

Martinus Martinus

unread,
Aug 30, 2012, 10:41:00 PM8/30/12
to mongod...@googlegroups.com
Hi Jenna,

I used below java code :

int animal_id = 1;
Date current = new Date(System.currentTimeMillis());
String animal_species = "dog";
String animal_name = "Dingo";
String animal_food = "meat";
Date birthday =  new Date(2009, 10, 1);

// Animal meals
ArrayList<BasicDBObject> meals = new ArrayList<BasicDBObject>();
BasicDBObject meals_desc = new BasicDBObject();
meals_desc.put("desc", "meat");
meals.add(meals_desc);

// animal house number
ArrayList<BasicDBObject> house_numbers = new ArrayList<BasicDBObject>();
String[] training_types = new String[2];
training_types[0] = "jumping";
training_types[1] = "sitting";
house_numbers.add((BasicDBObject) BasicDBObjectBuilder.start()
                .add("_id", 2)
                .add("date", new Date(System.currentTimeMillis()))
                .add("trainer", "Ken")
                .add("training_types", training_types)
                .add("state", "Still learning")
                .add("progress", 0.7)
                .get());

// animal skills
BasicDBObject skills = new BasicDBObject();
skills.put("jumping", 0.3);
skills.put("sitting", 0.4);
skills.put("smiling", 0.0);
skills.put("fetching", 0.0);

// animal friends
ArrayList<BasicDBObject> friends = new ArrayList<BasicDBObject>();
friends.add((BasicDBObject) BasicDBObjectBuilder.start()
                .add("_id", 3)
                .add("name", "Dig")
                .add("cage_num", 7)
                .add("status",  "good friend")
                .get());

// animal behaviors
ArrayList<BasicDBObject> behaviors = new ArrayList<BasicDBObject>();
BasicDBObject reactions = new BasicDBObject();
reactions.put("water", "playful");
reactions.put("wind", "open mouth");
reactions.put("tennis_ball", "wiggle tail");
behaviors.add((BasicDBObject) BasicDBObjectBuilder.start()
                .add("date", new Date(2012, 5, 10))
                .add("aggressiveness_level", 0.2)
                .add("reactions", reactions)
                .add("sleep_time",  "night")
                .get());

// animal diseases
BasicDBObject inner = new BasicDBObject();
inner.put("stomach", "none");
inner.put("throat", "none");
inner.put("teeth", "none");
inner.put("eyes", "none");
inner.put("bones", "none");

BasicDBObject outer = new BasicDBObject();
outer.put("skin", "none");
outer.put("nails", "none");
outer.put("fur", "none");

BasicDBObject diseases = new BasicDBObject();
diseases.put("inner", inner);
diseases.put("outer", outer);

// animal owner
BasicDBObject skills_wanted = new BasicDBObject();
skills_wanted.put("sitting", "good");
skills_wanted.put("fetching", "good");
skills_wanted.put("jumping", "good");

ArrayList<BasicDBObject> owner = new ArrayList<BasicDBObject>();
owner.add((BasicDBObject) BasicDBObjectBuilder.start()
                .add("_id", 5)
                .add("complaint", "none")
                .add("job", "grease monkey")
                .add("skills_wanted",  skills_wanted)
                .get());

// animal overall data
BasicDBObject animal_data = new BasicDBObject();
animal_data.put("_id", animal_id);
animal_data.put("date", current);
animal_data.put("species", animal_species);
animal_data.put("name", animal_name);
animal_data.put("food", animal_food);
animal_data.put("birthday", birthday);
animal_data.put("house_numbers", house_numbers);
animal_data.put("skills", skills);
animal_data.put("friends", friends);
animal_data.put("behaviors", behaviors);
animal_data.put("diseases", diseases);
animal_data.put("owner", owner);

// insert the document
animal_coll.insert(animal_data);

And I checked it in mongos console using below command :

animal_coll.count()

and it gave me 0;

Thanks.

Jenna

unread,
Aug 31, 2012, 2:22:18 PM8/31/12
to mongod...@googlegroups.com
Hi Martinus,

Are you using safe writes? If not, could you use getLastError() to see if there was an error that occurred when you tried to insert the document? 

Martinus Martinus

unread,
Sep 2, 2012, 10:26:46 PM9/2/12
to mongod...@googlegroups.com
Hi Jenna,

I don't use safe writes, do I need to use it with getLastError()? So my code will looks like below :

m.setWriteConcern(WriteConcern.SAFE);

// insert the document
animal_coll.insert(animal_data);

Then it will automatically give me the error?

If I run the db.runCommand("getlasterror") directly from console after I finished inserting it gave me "{ "err" : null, "ok" : 1 }".

Thanks.

--

Jenna

unread,
Sep 4, 2012, 10:12:57 AM9/4/12
to mongod...@googlegroups.com
What happens when you do .find() in animal_coll? Do you see the document that you inserted? Can you give some details about your environment- are you sharded? Can you run db.stats() and db.animal_coll.stats() in the shell and post the output?


On Thursday, August 30, 2012 6:19:42 AM UTC-4, Martinus Martinus wrote:

Martinus Martinus

unread,
Sep 4, 2012, 10:22:38 PM9/4/12
to mongod...@googlegroups.com
Hi Jenna,

If I do .find() inside animal_coll it will gave me null as it's result. The database itself is sharded but the collection no. If I run db.animal_coll.stats() it gave me like below :

{
"sharded" : false,
"primary" : "clusterA",
"ns" : "animal.animal_coll",
"count" : 0,
"size" : 0,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 3,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 24528,
"indexSizes" : {
"_id_" : 8176,
"species" : 8176,
"name" : 8176
},
"ok" : 1
}

and db.stats() :
{
"raw" : {
"clusterA/set1:10001,set1:10002,set1:10003" : {
"db" : "animal",
"collections" : 28,
"objects" : 760914,
"avgObjSize" : 981.9182719729167,
"dataSize" : 747155360,
"storageSize" : 1429651456,
"numExtents" : 133,
"indexes" : 39,
"indexSize" : 58131360,
"fileSize" : 14958985216,
"nsSizeMB" : 16,
"ok" : 1
},
"clusterB/set2:10004,set2:10005,set2:10006" : {
"db" : "animal",
"collections" : 13,
"objects" : 286065,
"avgObjSize" : 1832.697659622813,
"dataSize" : 524270656,
"storageSize" : 636481536,
"numExtents" : 36,
"indexes" : 12,
"indexSize" : 47412624,
"fileSize" : 6373244928,
"nsSizeMB" : 16,
"ok" : 1
}
},
"objects" : 1046979,
"avgObjSize" : 1214.3758528108015,
"dataSize" : 1271426016,
"storageSize" : 2066132992,
"numExtents" : 169,
"indexes" : 51,
"indexSize" : 105543984,
"fileSize" : 21332230144,
"ok" : 1
}

Thanks.


--

Jenna

unread,
Sep 5, 2012, 10:41:43 AM9/5/12
to mongod...@googlegroups.com
Hi Martinus,

Are you connecting to the cluster and inserting the documents via mongos? Could you post all of your code so that we can attempt to reproduce the issue?

Thanks,
Jenna


On Thursday, August 30, 2012 6:19:42 AM UTC-4, Martinus Martinus wrote:

Martinus Martinus

unread,
Sep 6, 2012, 12:10:54 AM9/6/12
to mongod...@googlegroups.com
Hi Jenna,

Yes I connect and inserting document via mongos. Actually, the above source code is already the source code that I used.

Thanks.

--
Reply all
Reply to author
Forward
0 new messages