From 3529871c0c317e59b084b00286e0ed12fadf5508 Mon Sep 17 00:00:00 2001 From: yinlainghui Date: Thu, 28 Mar 2019 17:53:19 +0800 Subject: [PATCH] =?UTF-8?q?mongo=20drive=20=E4=B8=AD=20find=20fields?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/objectql/src/driver/mongo.ts | 17 ++++-- .../objectql/test/unit/driver/mongo/fields.ts | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 packages/objectql/test/unit/driver/mongo/fields.ts diff --git a/packages/objectql/src/driver/mongo.ts b/packages/objectql/src/driver/mongo.ts index a98ed2eeba..e1eaf4929f 100644 --- a/packages/objectql/src/driver/mongo.ts +++ b/packages/objectql/src/driver/mongo.ts @@ -48,10 +48,21 @@ export class SteedosMongoDriver implements SteedosDriver { } /* TODOļ¼š */ - getMongoOptions(filters: SteedosQueryOptions){ - return { - //projection: {_id: 1} + getMongoOptions(options: SteedosQueryOptions): JsonMap { + if (_.isUndefined(options)) { + return {} } + let fields: string[] = options.fields; + if (_.isUndefined(fields)) { + return {} + } + let result: JsonMap = {}; + let projection: JsonMap = {}; + fields.forEach((field)=>{ + projection[field] = 1; + }); + result.projection = projection; + return result; } collection(name: string) { diff --git a/packages/objectql/test/unit/driver/mongo/fields.ts b/packages/objectql/test/unit/driver/mongo/fields.ts new file mode 100644 index 0000000000..40a0f2ecfa --- /dev/null +++ b/packages/objectql/test/unit/driver/mongo/fields.ts @@ -0,0 +1,52 @@ +import { SteedosMongoDriver } from "../../../../src/driver"; +import { expect } from 'chai'; + +let tableName = "mongo-driver-test-fields"; + +describe('fetch records width specific fields', () => { + before(async ()=>{ + let mongo = new SteedosMongoDriver({ url: "mongodb://127.0.0.1/steedos" }); + await mongo.connect(); + await mongo.collection(tableName).deleteMany() + }); + + it('fetch only some specific fields', async () => { + + let mongo = new SteedosMongoDriver({ url: "mongodb://127.0.0.1/steedos" }); + await mongo.connect(); + await mongo.insert(tableName, { _id: "ptr", name: "ptr", title: "PTR" }) + await mongo.insert(tableName, { _id: "cnpc", name: "cnpc", title: "CNPC" }) + + let queryOptions = { + fields: ["name"] + }; + let result = await mongo.find(tableName, queryOptions); + console.log("fetch records width specific fields result:"); + console.log(result); + + await mongo.delete(tableName, "ptr"); + await mongo.delete(tableName, "cnpc"); + expect(result).to.be.length(2); + expect(result[0].name).to.be.eq("ptr"); + expect(result[0].title).to.be.eq(undefined); + }); + it('fetch all fields', async () => { + + let mongo = new SteedosMongoDriver({ url: "mongodb://127.0.0.1/steedos" }); + await mongo.connect(); + await mongo.insert(tableName, { _id: "ptr", name: "ptr", title: "PTR" }) + await mongo.insert(tableName, { _id: "cnpc", name: "cnpc", title: "CNPC" }) + + let queryOptions = { + }; + let result = await mongo.find(tableName, queryOptions); + console.log("fetch records width specific fields result:"); + console.log(result); + + await mongo.delete(tableName, "ptr"); + await mongo.delete(tableName, "cnpc"); + expect(result).to.be.length(2); + expect(result[0].name).to.be.eq("ptr"); + expect(result[0].title).to.be.eq("PTR"); + }); +});