1

After a lot of reading, I am stuck. the code I posted here, is the implementation of a store database I am trying to make. in every store, we have some fields. I am interested in doing something with the items array, that contains JSON variables. I want to filter the items through three filters, firstly by the store ID, secondly by the category ID, and the last filter will be the semi category ID.

I want to send the data from the front end, meaning I supply STOREID, the CategoryID, and the SemiCategoryID. after receiving the data at the back end side, I am expecting to receive only the relevant items according to the data supplied by the front end.

{
    "_id": {
        "$oid": "5a1844b5685cb50a38adf5bb" --> **ID of the STORE**
    },
    "name": "ACE",
    "user_id": "59e4c41105d1f6227c1771ea",
    "imageURL": "none",
    "rating": 5,
    "items": [
        {
            "name": "NirCohen",
            "categoryID": "5a0c2d292235680012bd12c9",
            "semiCatID": "5a0c2d5a2235680012bd12ca",
            "_id": {
                "$oid": "5a1958181cd8a208882a80f9"
            }
        },
        {
            "name": "he",
            "categoryID": "5a0c2d292235680012bd12c9",
            "semiCatID": "5a0c2d5a2235680012bd12ca",
            "_id": {
                "$oid": "5a1973c40e561e08b8aaf2b2"
            }
        },
        {
            "name": "a",
            "categoryID": "5a0c2d292235680012bd12c9",
            "semiCatID": "5a0c2d5a2235680012bd12ca",
            "_id": {
                "$oid": "5a197439bc1310314c4c583b"
            }
        },
        {
            "name": "aaa",
            "categoryID": "5a0c2d292235680012bd12c9",
            "semiCatID": "5a0c2d5a2235680012bd12ca",
            "_id": {
                "$oid": "5a197474558a921bb043317b"
            }
        },
    ],
    "__v": 9
}

and I want the Backend to return the filtered items according to the query. The problem is, I am not managing to get the CORRECT query.

your help will be much appreciated,

thank you in advance.

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
nir cohen
  • 11
  • 1
  • 4
  • Could you try to explain better what it is you want to happen? Where is the function calling mongo? What is your result and what is your intended result? – OArnarsson Nov 26 '17 at 22:17
  • currently there is a basic query mongoose that pop only the store, i want to get all the 'items' (names) that match the categoryID and subcategoryID. I want to get a json list\array with all the items (name field) that match to the store ID (that i successed already) and match to the other parameters (categoryID, semiCatyegoryID) – nir cohen Nov 27 '17 at 09:16
  • So you are fetching an object, only get the storeID but you want to populate the Store object? check this out http://mongoosejs.com/docs/populate.html – OArnarsson Nov 27 '17 at 09:20
  • Hi, i got the storeID, categoryID and samiCategoryID i just need to know to query to filter by this id's. – nir cohen Nov 27 '17 at 17:22
  • Hi, @nircohen can you check this out -> https://stackoverflow.com/questions/21274306/mongoose-how-to-do-a-find-with-two-or-conditions and on the place of `or` you need to use `and`. – Utkarsh Dubey Nov 28 '17 at 05:55
  • Multiple query example -> https://stackoverflow.com/questions/13272824/combine-two-or-queries-with-and-in-mongoose – Utkarsh Dubey Nov 28 '17 at 05:56

2 Answers2

3

If I understand you correctly, you are doing something like this:

Store.find({}).then(..);

If you only want to find the stores where categoryID is equal to the variable myCategory, you could filter them out by using:

Store.find({semiCatID: myCategory}).then(..);

Please let me know if this is not what you are after, then we can keep trying to figure this out together.

EDIT: So you are sending the variables StoreID, CategoryID and SemiCategoryID from the frontend. Receive them in the backend, and want to filter your database collection matching all three fields? If so.. then I think all you have to do is change your current query:

store.findOne({ _id: req.body.userID }, (err, store) => { console.log(store); }); 

To something like:

store.findOne({
    _id: req.body.userID,
    storeID: req.body.StoreID,
    categoryID: req.body.CategoryID,
    semiCategoryID: req.body.SemiCategoryID
}, (err, store) => { console.log(store); }); 

This way, the objects you get back from mongo must match all four criterias given from the frontend.

OArnarsson
  • 801
  • 1
  • 9
  • 25
  • Hi, first thanks. I eddited my post in order to make my question more clearly. For now this is my qurey .... store.findOne({ _id: req.body.userID }, (err, store) => { console.log(store); }); – nir cohen Nov 27 '17 at 23:35
  • @nircohen I've edited the answer, hopefully to achieve your desired results. – OArnarsson Nov 28 '17 at 10:02
  • I tried you query, if you didnt saw the categoryID and semiCategoryID there are in the array that call 'items' so should i use items.categoryID and items.semiCategoryID ? Thanks alot for help! – nir cohen Nov 28 '17 at 13:19
  • in the query I tried to do this ""items.categoryID": req.body.category," and its not working. any ideas? – nir cohen Nov 28 '17 at 17:14
1

As far as I Understood your question here is my answer to it you can use findById

Store.findById({//store id}).then(..);

or

Store.findOne({_id:ObjectID(storeID)}).then(..);
Vignesh
  • 1,140
  • 1
  • 9
  • 17
  • I dont understand your answer. because, I dont need to filter by the 3 IDs? (Store,Category,SemiCategory) as i see its looks like your answer will give me only the store values.. no? – nir cohen Nov 28 '17 at 09:08