猫鼬在数据库中找不到元素

时间:2018-11-05 15:02:00

标签: javascript node.js mongodb mongoose

我的MongoDB数据库上有一个表示品牌的特定模式

var BrandSchema = new Schema({
  name: {type: String, required: true, unique: true},
  peopleInterested: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
}).plugin(mongoosePaginate);

现在,已经通过mongo shell使用命令db.Brand.insert({})填充了此集合,但是,如果我尝试再次通过shell或通过我的Node.JS应用程序Node.js来查询集合,即使有匹配的元素也不会返回任何内容。
我以为定义peopleInterested数组的方式可能有问题,因此我在没有关系引用的情况下重新定义了Schema,但是我仍然遇到相同的问题。  
以下是用户架构的示例

var UserSchema = new Schema({
  isAdmin: {type: Boolean, default: false},
  name: String,
  surname: String,
  email: { type: String, lowercase: true, required: true, trim: true, unique: true, dropDubs: true },
  password: { type: String, required: true },
  salt: { type: String },
  verified: { type: Boolean, default: false },
  bio: {
    type: { type: String, enum: [0,1] }, // 0='Squadra', 1='Giocatore'
    birthday: String,
    height: Number,
    number: Number,
    role: { type: String, enum: [0,1,2,3] }, // 0='Playmaker', 1='Ala', 2='Guardia', 3='Centro'
    team: String,
    city: String,
    fiscalCode: {type: String, maxlength:16}
  },
  newsletter: {type: Boolean, default: false},
  lastCheckin: {type: mongoose.Schema.Types.ObjectId, ref: 'Checkin'},
  follows: [{type: mongoose.Schema.Types.ObjectId, ref: 'Structure'}],
  interestedBrands: Array,
  score: { type: Number, default: 0 },
  profilePicture: String,
  lastLogin: {type: Date},
  facebook: {
    id: String,
    accessToken: String,
    profileImage : String
  }
}, {
  collection: 'users',
  retainKeyOrder: true,
  timestamps: true,
}).plugin(mongoosePaginate);

我想念什么?
编辑:这是我通过外壳或通过Node.JS执行的两个示例查询
Shell:db.Brand.find({name:"Canterbury"})
Node.js:

exports.findByName = (req, res) => {
    Brand.find({name: req.body.name},function(err, results) {
    return err ? res.status(404).json(err) : res.status(202).json(results);
  });
};


编辑2:一个mongo shell返回的示例(根据注释的要求)

{ "_id" : ObjectId("5be05876f2daa245bf1d33e4"), "name" : "Canterbury", "peopleInterested" : [ ] }


编辑3:通过shell返回的db.Brand.find({})查询示例

> db.Brand.find({})
{ "_id" : ObjectId("5be05876f2daa245bf1d33e4"), "name" : "Canterbury", "peopleInterested" : [ ] }
{ "_id" : ObjectId("5be0587ff2daa245bf1d33e5"), "name" : "Adidas", "peopleInterested" : [ ] }
{ "_id" : ObjectId("5be05887f2daa245bf1d33e6"), "name" : "Nike", "peopleInterested" : [ ] }

0 个答案:

没有答案