B
B
bumbay2014-09-19 13:35:35
Mongoose
bumbay, 2014-09-19 13:35:35

Extract data from a collection by array?

There is.

/* 0 */
{
    "_id" : ObjectId("541c03489c3009e80de19d6b"),
    "ts" : 1411121993,
    "close" : false,
    "users" : [ 
        "7e8f5f8fc847bc6", 
        "920299d58dc02ec"
    ],
    "__v" : 0
}

/* 1 */
{
    "_id" : ObjectId("541c035f9c3009e80de19d6c"),
    "ts" : 1411122016,
    "close" : false,
    "users" : [ 
        "7e8f5f8fc847bc6", 
        "a285496ba35fe98"
    ],
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("541c035f9c3009e80de19d6d"),
    "ts" : 1411122016,
    "close" : false,
    "users" : [ 
        "7e8f5f8fc847bc6", 
        "c2f1f4e949237d1"
    ],
    "__v" : 0
}

This is a collection of dialogues.
Let's say my id is 7e8f5f8fc847bc6
I need to create a new dialog with user id - c2f1f4e949237d1
To do this, I need to check for the existence of such a dialog.
If it already exists and close: false, then you need to issue its dialog id, if not, create a new one.
The problem is that I don't understand how to make a validation request in mongoose.
The data in the users array can be in any order , but there cannot be more than two.
In sql it looks like this:
select ... where (users = ['c2f1f4e949237d1', '7e8f5f8fc847bc6'] or users = ['7e8f5f8fc847bc6', 'c2f1f4e949237d1']) and close = false

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question