NoRM - FindAndModify with arguments

720 views Asked by At

Is it possible to specify fields while doing a FindAndModify, so only one field is returned?

Also, is it possible to do an upsert, to create the object if it doesn't exist.

As per: http://www.mongodb.org/display/DOCS/findAndModify+Command

I can't see any way of adding the additional arguments

EDIT: Seems to be some confusion - I am using NoRM (C#) https://github.com/atheken/NoRM/

2

There are 2 answers

7
Nekresh On BEST ANSWER

I'm afraid it's not possible actually in NoRM. You could fork the project and add an overloaded FindAndModify method to the file NoRM/Collections/MongoCollectionGeneric.cs to support this behavior.

I think you might need to add a field fields in the anonymous object passed to findOne.

var returnValue = cmdColl.FindOne(new
{
  findandmodify = this._collectionName,
  query = query,
  update = update,
  sort = sort,
  fields = fields
}).Value;

And maybe a pull request :)

1
Sridhar On

Use the fields specifier. e.g.

db.foo.findAndModify({query:{_id:"myid"},
update:{$set:{priority:78}},new:true,fields:{_id:1,priority:1}})