Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何使用mongoose返回完整对象_Mongodb_Mongoose - Fatal编程技术网

Mongodb 如何使用mongoose返回完整对象

Mongodb 如何使用mongoose返回完整对象,mongodb,mongoose,Mongodb,Mongoose,我想知道是否有一种方法可以检索完整的文档(即使使用未定义的和空的键) 这是我的模式: var userSchema = new Schema({ username : {type: String, index: {unique: true, dropDups: true}} , password : String , email : {type: String, index: {unique: true, dropDups: true}} ,

我想知道是否有一种方法可以检索完整的文档(即使使用未定义的和空的键)

这是我的模式:

var userSchema =  new Schema({
  username      : {type: String, index: {unique: true, dropDups: true}} ,
  password      : String ,
  email         : {type: String, index: {unique: true, dropDups: true}} ,
  gender        : String 
}) 
假设某个用户的性别没有定义,当我查询时,我只得到用户名、密码和电子邮件。。我怎么也能知道性别


抱歉,如果有任何错误的技术术语。

关键是在类型定义中使用
default
属性来提供默认值

var userSchema =  new Schema({
  username      : {type: String, index: {unique: true, dropDups: true}} ,
  password      : String ,
  email         : {type: String, index: {unique: true, dropDups: true}} ,
  gender        : {type: String, default: "Unknown" } 
}) 

有关更多信息,文档为。

如果没有性别,为什么会有性别?我想检索整个文档,以便将其作为json发送到我的模板。。如果性别不存在,则会导致错误。。我不想在前端处理这个问题。我知道这不是mongodb的工作方式。但这就是我的目的!