尝试获取文档时缺少子文档

时间:2017-01-25 13:00:22

标签: node.js mongodb mongoose mongodb-query

问题: 我正在创建一个包含模式的帐户表,如下所示。我只有一个帐户文档,供在我的localhost中运行的mongo服务器中的用户使用。在定义架构并添加单个帐户文档之后,我编写了一个端点来使用id键查询和获取该文档(Model.find(id: '12345'))。生成的JSON文档包含除了名为accRef的子文档之外的所有数据,如下面的Schema所示。当我使用Mongo Shell尝试相同的查询查询时,我得到了完美的结果。我一直试图找到导致这种情况持续近一整天的事情,但没有取得多大进展。任何帮助都将受到高度赞赏。

我开始使用空架构并逐个添加每个字段,从id属性开始并触发查找查询。每一次,我都获得了所有75+元素和子文档,但是当我将accRef subdoc添加到我的模式时,这条信息就会从结果中消失。

mongoose架构中可以定义的子文档数量是否有限制?

此外,我似乎无法使用如下所示的相同架构插入/保存任何新帐户对象。但是,我可以使用像MongoChef这样的客户端使用完全相同的JSON对象执行此插入/保存操作。

这是我的JS模块,我已经定义了我的模式:

import mongoose from 'mongoose';
mongoose.connect('mongodb://localhost:27017/accounts');

const Schema = mongoose.Schema;

const accountsSchema = new Schema({
  _id: Schema.ObjectId,
  id: String,
  uuid: String,
  firstName: String,
  lastName: String,
  contactNumber: Number,
  dob: Date,
  email: String,
  originalEmail: {type: String, index: true},
  password: String,
  socialMedia: {
    facebook: {
      id: String,
      email: String,
      token: String
    }
  },
  domainInfo: {
    country: String,
    currency: String,
    lang: String,
    domain: String
  },
  profile: {
    type: Number,
    id: Number,
    points: Number,
    accumulated: Number
  },
  perks: {
    id: Number,
    type: Number
  },
  address: {
    address: String,
    postalCode: String
  },
  paymentInfo: {
    cardNumber: String,
    isValidCard: Number,
    paypal: {
      email: String
    },
    bank: {
      bankName: String,
      bankNumber: String,
      accontId: Number,
      accountHolderName: String,
      bankBranchCode: String,
      paymentTypeId: Number
    },
    dateCreated: {type: Date, default: Date.now},
    dateModified: {type: Date, default: Date.now},
    createdAt: {type: Date, default: Date.now},
    updatedAt: {type: Date, default: Date.now}
  },
  metadata: {
    timezone: String,
    ipAddress: String,
    signupMeta: String,
    clientUserAgent: String,
    signupUrl: String
  },
  accRef: {
    type: String,
    code: String,
    url: String
  },
  key: String,
  parentId: String,
  type: String,
  utm: String,
  bounced: Number,
  status: Number,
  isPoints: Number,
  isAdmin: String,
  dateTutorialCompleted: {type: Date, default: Date.now},
  dateCreated: {type: Date, default: Date.now},
  dateModified: {type: Date, default: Date.now},
  dateAccessed: {type: Date, default: Date.now},
  dateVerified: {type: Date, default: Date.now},
  datePurchased: Date,
  createdAt: {type: Date, default: Date.now},
  updatedAt: {type: Date, default: Date.now}
});

const Account = mongoose.model('Account', accountsSchema);

export default Account;

激活mongo查询的服务类中的函数:

import Account from '../../models/mongo';

const getAccountById = (accountId) => {
  return Account.find({id: accountId})
    .then(response => {
      if(response.length === 0) {
        throw new Error("Account Not Found");
      }
      console.log(response);
      return response;
    })
    .catch(error => {
      throw new Error(error.message);
    });
};

猫鼬版尝试使用: 4.7.7 4.7.8 (尝试过两者)

MongoDB版尝试使用: 3.2 3.4 (尝试过两者)

节点版本 7.4.0

0 个答案:

没有答案