0

I want to do this in ColdFusion

RecurringType = rc.db.RecurringType
    .find()
    .sort({ "SortOrder" : 1 })
    .project({_id : 0})
    .toArray();

However I get an error message of

    The project method was not found.

I am currently using

RecurringType = rc.db.RecurringType
    .find()
    .sort({ "SortOrder" : 1 })
    .toArray()
    
RecurringType.each( (i) => { i.delete("_id") } )

As a workaround. I want to do this directly

4
  • What is project() meant to do? I'm not familiar with it as a built-in function, so it seems you'd need to define it. And how is this related to mongodb? Commented Jul 30 at 15:09
  • It is built into mongodb cli. It is used to filter which pieces of data get return. I don't necessarily want all fields. See: mongodb.com/docs/manual/tutorial/… Commented Jul 31 at 0:19
  • QueryMap() might help here, or use a Query of Queries as an intermediate step. Commented Jul 31 at 13:20
  • You can pass the projection as an argument to find: mongodb.com/docs/manual/reference/method/db.collection.find Commented Aug 2 at 23:45

1 Answer 1

0

I think the issue is how you're doing the projection. Find in MongoDB uses the following syntax.

db.collection.find( <query>, <projection>, <options> )

Where <query> is the equivalent of the Where directive in SQL, and <projection> is the list of fields to be returned. So what you want is something like this:

RecurringType = rc.db.RecurringType
    .find(
         {}, // no filtering
         {_id : 0} // return all except the id
    )
    .sort({ "SortOrder" : 1 }) // sort by SortOrder ascending
    .toArray(); // convert the object to an array of structs.

Please note I haven't directly tested this, but here's an example I know that works:

var isoDate = DateTimeFormat(parseDateTime(arguments.deltaDate), "iso"); // convert the last change argument to an isoformat.

var filter =  { "lastChange": { "$gte": { "$date": isoDate } } } ; 
// Specifies selection filter using query operators. 
// To return all documents in a collection, omit this parameter 
// or pass an empty document ({}).

var projection = {"_id": 0}; // don't include the _id in the returned 
// Specifies the fields to return in the documents that match the query filter.

var seasons = db.seasons.find(
    filter,
    projection
)
.sort({creationDate: -1}) // sort by creation date, newest first
.toArray(); // use the built in toArray() method to convert the Mongo cursor results to an array of structs.

HTH.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.