Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projection? #117

Open
fyn-dev opened this issue Mar 22, 2018 · 2 comments
Open

Projection? #117

fyn-dev opened this issue Mar 22, 2018 · 2 comments
Labels
question This issue is a question about the usage or behaviour of the library

Comments

@fyn-dev
Copy link

fyn-dev commented Mar 22, 2018

I don't how such query in Iridium will work
.find( { field: value }, { array: {$slice: count } } )
I don't see any support for projection. How to use?

@Kaffiend
Copy link

The aggregation framework is probably more suited to a search query, bit of my own example below in a repository pattern. The allowDiskUse is for a large query, you may not need it.

@injectable()
export class NaRecordRepository implements NaRecordRepository {

    public async search(query: any): Promise<any> {
        const queryResult = await naRecordsDb.connect().then(() => naRecordsDb.NaRecords.aggregate([
            {
                $match: {
                    $text: {
                        $search: query.term
                    },
                    date: {
                        $gte: new Date(query.startDate),
                        $lt: new Date(query.endDate)
                    },
                    indexId: { $in: query.indexes },
                    docType: { $in: query.docTypes },
                    restrictIndex: false,
                    restrictImage: { $in: query.includeRestrictedImages },
                    indexedFlag: { $in: query.includePartialIndex }
                }
            },
            {
                $sort: {
                    date: query.dateSort,
                }
            },
            {
                $project: {
                    nameSSN: 0,
                    nameCode: 0,
                    firstName: 0,
                    middleName: 0,
                    lastName: 0,
                    nameDescription: 0,
                    nameIndirect: 0,
                    nameClass: 0,
                    inputId: 0,
                    changeId: 0
                }
            }
        ], {
            allowDiskUse: true
        }));
        return queryResult;
    }

But the query you are showing, would be something like this...

public async findNaRec(rec: NaRecordDTO): Promise<NaRecordDTO[]> {
        const naRecval = await naRecordsDb.connect().then(() => naRecordsDb.NaRecords.find({lastName: rec.lastName}, {_id: 0})).then(val => {
            return val.toArray();
        });
        return naRecval;
    }

@notheotherben
Copy link
Member

notheotherben commented Mar 26, 2018

Thanks @Kaffiend - the aggregation pipeline is certainly a very powerful tool and well suited to these types of complex projection.

Additionally, if you simply want to provide a basic projection for a standard find query (as you would on the MongoDB CLI) then you can leverage the find(conditions, fields) overload of the Model.find method.

So let's say you have the following document in MongoDB:

{
  field: "value",
  array: [
    1,
    2,
    3,
    4
  ]
}

Running myModel.find({ field: "value" }, { array: { $slice: 2 } }).toArray() will resolve (it's a Promise) to the following:

[
  {
    _id: ".....",
    field: "value",
    array: [1,2]
  }
]

I hope that helps you @fyn-dev - but please let me know if you run into any issues with it.

@notheotherben notheotherben added the question This issue is a question about the usage or behaviour of the library label Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question This issue is a question about the usage or behaviour of the library
Projects
None yet
Development

No branches or pull requests

3 participants