2

Haven't change my code for some time, and it was working fine. But started yesterday, I found I can't save my new record to the mongod db via mongoid.

Background:

gem 'rails', '3.2.5'
gem 'mongo', "1.6.2"
gem "mongoid", "2.4.7"

mongo server version: db version v2.0.5, pdfile version 4.5 MongoDB shell version: 2.0.5

rails models and data size (still in development mode): 3 models (users: 164K, relation: 122M, status:370K)

Problem: Can't save new users records, as showing here, even it passed the contraints (like I have unique uid, id_str).

[2] pry(main)> User.count
=> 164325
[3] pry(main)> u=User.new(uid: 1001, id_str: "1001")
=> #<User _id: 501452f346b0a14fb5000001, _type: nil, created_at: nil, updated_at: nil, uid: 1001, id_str: "1001", name: nil, screen_name: nil, location: nil>
[4] pry(main)> u.save
=> true
[5] pry(main)> User.count
=> 164325
[6] pry(main)> u.save!
=> true
[7] pry(main)> User.count
=> 164325

Rails log file:

MONGODB (0ms) blog_development['$cmd'].find({"count"=>"users", "query"=>{}, "fields"=>nil}).limit(-1)
MONGODB (0ms) blog_development['users'].insert([{"_id"=>BSON::ObjectId('501452f346b0a14fb5000001'), "uid"=>1001, "id_str"=>"1001", "updated_at"=>2012-07-28 21:00:39 UTC, "created_at"=>2012-07-28 21:00:39 UTC}])
MONGODB (0ms) blog_development['$cmd'].find({"count"=>"users", "query"=>{}, "fields"=>nil}).limit(-1)
MONGODB (0ms) blog_development['$cmd'].find({"count"=>"users", "query"=>{}, "fields"=>nil}).limit(-1)

Nothing in the mongodb.log file as the operation is so fast

What could be the reason causing the not-saving issue?

updates: My db status in mongo shell:

"ns" : "blog_development.users",
    "count" : 164325,
    "size" : 159656080,
    "avgObjSize" : 971.5872813022972,
    "storageSize" : 177336320,
    "numExtents" : 14,
    "nindexes" : 3,
    "lastExtentSize" : 36089856,
    "paddingFactor" : 1.0099999999963194,
    "flags" : 1,
    "totalIndexSize" : 22729280,
    "indexSizes" : {
        "_id_" : 9598624,
        "uid_1" : 6091120,
        "screen_name_1" : 7039536
    },
    "ok" : 1

os is: 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:24 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

3
  • What does the DB shell output of db.users.stats() look like? and what O/S are you using? Commented Jul 28, 2012 at 21:56
  • updated in the question. seems fine in my db. Commented Jul 28, 2012 at 22:50
  • just give a try using create, is it able to save to db. I was not able to get the soln :( Commented Jul 29, 2012 at 11:16

1 Answer 1

4

You are using the save method which follows "upsert" semantics. If there is an existing document that can be found matching your criteria ((uid: 1001, id_str: "1001")), it will be updated instead of inserting a new document.

 u=User.new(uid: 1001, id_str: "1001")

 u.save
=> true

Since you actually intended to create a new document, you should be using insert:

u.insert

Alternatively, you can also allow a custom _id in your class and specify this on creation. You'll need to override Mongoid's _id field which is readonly by default.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, there were a duplicated record in my database, find out that later. so you were right. Should distinguish "save" and "insert". Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.