MongoDB,find()结果,转换为json

时间:2015-10-14 10:56:21

标签: json mongodb

email: {"email":"aaa@bbb.eu"}
{ _mongooseOptions: {},
  mongooseCollection:
   { collection:
      { db: [Object],
        collectionName: 'parties',
        internalHint: null,
        opts: {},
        slaveOk: false,
        serializeFunctions: false,
        raw: false,
        pkFactory: [Object],
        serverCapabilities: undefined },
     opts: { bufferCommands: true, capped: false },
     name: 'parties',
     conn:
      { base: [Object],
        collections: [Object],
        models: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'pluserDB',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        _events: {},
        db: [Object] },
     queue: [],
     buffer: false },
  model:
   { [Function: model]
     base:
      { connections: [Object],
        plugins: [],
        models: [Object],
        modelSchemas: [Object],
        options: [Object] },
     modelName: 'Party',
     model: [Function: model],
     db:
      { base: [Object],
        collections: [Object],
        models: [Object],
        replica: false,
        hosts: null,
        host: 'localhost',
        port: 27017,
        user: undefined,
        pass: undefined,
        name: 'pluserDB',
        options: [Object],
        otherDbs: [],
        _readyState: 1,
        _closeCalled: false,
        _hasOpened: true,
        _listening: true,
        _events: {},
        db: [Object] },
     discriminators: undefined,
     schema:
      { paths: [Object],
        subpaths: {},
        virtuals: [Object],
        nested: {},
        inherits: {},
        callQueue: [],
        _indexes: [],
        methods: {},
        statics: {},
        tree: [Object],
        _requiredpaths: undefined,
        discriminatorMapping: undefined,
        _indexedpaths: undefined,
        options: [Object],
        _events: {} },
     options: undefined,
     collection:
      { collection: [Object],
        opts: [Object],
        name: 'parties',
        conn: [Object],
        queue: [],
        buffer: false } },
  op: 'find',
  options: {},
  _conditions: { email: '{"email":"aaa@bbb.eu"}' },
  _fields: { email: 1, _id: 0 },
  _update: undefined,
  _path: undefined,
  _distinct: undefined,
  _collection:
   { collection:
      { collection: [Object],
        opts: [Object],
        name: 'parties',
        conn: [Object],
        queue: [],
        buffer: false },
     collectionName: undefined },
  _traceFunction: undefined,
  _castError: null }

请看下面的代码:

var parties = Party.find({ email: email }, { email: 1, _id: 0 });

此查询返回3个文档 现在我想发送结果并将这些文件字符串化:

response.json(parties);  

我在这里收到错误,因为parties包含循环引用,并且不仅包含我需要的纯数据。

问题是:

如何在此处对find()方法的结果进行字符串化?

1 个答案:

答案 0 :(得分:1)

我知道我的错误是什么。

MongoDB它的shell与Mongoose API不同。

find()方法需要两个不同的参数。

我需要的是:

Party.find({ 'email': email }, 'email', function (err, parties) { response.json(parties)});