0

I have the following query:

db.COLLECTIONNAME.update({ groupId:11 },{ $set: { "users.$[elem1].preferred_username": 'cmaronchick2'} },{ arrayFilters: [ {"elem1.username":'cmaronchick'} ] })

My Mongo server version is 3.6.6. I have tried this using the shell and it works.

However, when I try the same query using a Lambda function using Node.js on AWS, I get the following error:

No array filter found for identifier \'elem1\'

How do I resolve this?

Here is the connection code:

var mongo = require("mongodb").MongoClient,
assert = require("assert");

const MONGO_URL = 'mongodb://USERNAME:PASSWORD@MONGODBURL:PORT/DBNAME';

// console.log('Loading function');

exports.handler = (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));

mongo.connect(MONGO_URL, function (err, db) {
    db.COLLECTIONNAME.update({ groupId:11 },{ $set: { "users.$[elem1].preferred_username": 'cmaronchick2'} },{ arrayFilters: [ {"elem1.username":'cmaronchick'} ] })
}
2
  • how are you connecting to the database in your Lambda? maybe it is not connecting to the right one Commented Nov 8, 2018 at 20:06
  • Thanks. I added the connection code. I am able to connect and retrieve data from a different collection in the same code. I think it may have something to do with the version I'm using, but I don't know how to update it. Commented Nov 8, 2018 at 20:28

2 Answers 2

2

You could try to remove the DBNAME from the MONGO_URL and specifying the database after the connection to the server is done.

Per the documentation, the database specified in the connection string for mongo is the database that will be used to look for and authenticate the user trying to access the server, the default is admin

const MONGO_DB = DBNAME
const MONGO_URL = 'mongodb://USERNAME:PASSWORD@MONGODBURL:PORT';

mongo.connect(MONGO_URL, function (err, client) {
    let db = client.db(MONGO_DB) // <- Specifying the database
    db.COLLECTIONNAME.update({ groupId:11 },{ $set: { "users.$[elem1].preferred_username": 'cmaronchick2'} },{ arrayFilters: [ {"elem1.username":'cmaronchick'} ] })
}

It is also similar to how they do it in the official documentation

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

1 Comment

Thanks for the help!
0

Ok, this was a pretty silly mistake. Updating array elements was not compatible with the node_module version of mongo that I had uploaded (it was version 2.1.X and array filters were introduced in 3.1).

I updated my mongodb and mongodb-core versions to 3.1.9 and 3.1.8 (using npm update mongodb mongodb-core), respectively, and updated my code like so:

const myDB = db.db('DBNAME');
const profileCollection = myDB.collection('profile');
const groupsCollection = myDB.collection('groups');

groupsCollection.updateOne({ groupId:11 },
    { $set: { "users.$[elem1].preferred_username": 'cmaronchick1'} },
    { arrayFilters: [ {"elem1.username":'cmaronchick'} ] } )

And that worked.

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.