Dynamoose: How to query for multiple conditions

Created on 14 Dec 2017  路  3Comments  路  Source: dynamoose/dynamoose

Hi! How can I correctly scan for two attribute conditions in the same run? When adding a new user to the table, I would like to check if there is already any users with that email or username. I've tried chaining scans but it doesn't seem to work. Also tried the .or() keyword which seems to be only for readability? Putting both attributes in the same scan options would of course just return users that match both conditions.

            .scan({
                username: {eq: new_user.username}
             })
            .scan({
                email: {eq: new_user.email}
             })
            .exec()
            .then((duplicate_users) => {

                console.log(duplicate_users);
              //...

Currently I'm doing it this way:

        var p1 = User.scan('username').eq(new_user.username).count().exec();
        var p2 = User.scan('email').eq(new_user.email).count().exec();

        Promise.all([p1, p2])
            .then((user_counts) => { 
//...

Most helpful comment

@androidand - First sorry for the slow response.

User.scan(
  {
    username: {eq: new_user.username},
    email: {eq: new_user.email}
  }, 
  {conditionalOperator: 'OR'}
).exec()

or

User.scan('username').eq(new_user.username).or().where('email').eq(new_user.email).exec()

All 3 comments

Mongoose schema has the unique: true attribute. That would be nice for Dynamoose as well!

@androidand - First sorry for the slow response.

User.scan(
  {
    username: {eq: new_user.username},
    email: {eq: new_user.email}
  }, 
  {conditionalOperator: 'OR'}
).exec()

or

User.scan('username').eq(new_user.username).or().where('email').eq(new_user.email).exec()

Due to the lack of activity I'm closing this issue. Please feel free to comment if this did not solve your problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fishcharlie picture fishcharlie  路  7Comments

arthurvi picture arthurvi  路  7Comments

mogwai picture mogwai  路  5Comments

brennhill picture brennhill  路  10Comments

MickL picture MickL  路  6Comments