2014-09-06 58 views
0

我正在使用Mongoid for Ruby。我以前在我的Mongo數據庫中查詢過,但忘記了它與Mongo的工作方式。我有這樣的用戶模型:如何訪問Mongo數據庫控制檯中的用戶

class User 
    include Mongoid::Document 

    field :email, type String 
    field :crypted_password, type String 
    ... 
end 

我將如何抓住我的控制檯中的所有用戶或特定用戶?由於任何人誰可以幫助

回答

1
use *database 

這將返回一個特定的用戶:

db.getUser("*user") 

這將返回所有用戶

db.getUsers() 
+0

感謝您的回覆。我在我的Mongo shell中出現'Error:no such cmd'。我想像'db.Users.find(email:「[email protected]」)。first'就足夠了,但是nahh – andy4thehuynh 2014-09-06 02:30:43

0

如果你的類被稱爲User然後底層MongoDB的集合會users。所以,你會說這樣的話:

> db.users.find() // All users 
> db.users.find({ field: value }) // Users that match certain conditions 
> db.users.findOne({ _id: ObjectId('...') }) // Find one user with a specific id 
... 

詳細內容見的findfindOne文檔。

+0

Snap ..我只是在我的Mongo Shell中做了'show dbs',得到了兩個'example_development'數據庫......一個說(空),另一個是0.078GB。我做過'使用example_development'兩次......我是否將數據庫擰緊了? – andy4thehuynh 2014-09-06 02:37:12

+0

你不應該有兩個同名的數據庫,你確定你沒有錯過名字中的某些東西嗎? – 2014-09-06 03:23:53

相關問題