如何访问Mongo数据库控制台中的用户

如何访问Mongo数据库控制台中的用户

问题描述:

我正在使用Mongoid for Ruby。我以前在我的Mongo数据库中查询过,但忘记了它与Mongo的工作方式。我有这样的用户模型:如何访问Mongo数据库控制台中的用户

class User 
    include Mongoid::Document 

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

我将如何抓住我的控制台中的所有用户或特定用户?由于任何人谁可以帮助

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

如果你的类被称为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