Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose find and create multidimensional arrays

I'm trying to get the userid, and the array object that contains the specific location within locations.

What I want to accomplish is the following:

The query will return the array location's result.

If not userid exist at all, create it, then return the array of the matching location.

If the location id Number is not there. create a new one, then return the array of the matching location.

How can I accomplish this?

Current query:

 easyCrime.findOne({
        userid: userid,
        "locations.location": location
    }, {"locations.$.location": 1}).then(function (err, stats) {


        }

    });

model:

   userid: {
        type: String,
        default: '57c1c0f3b6b20c011242bf22'
    },
    locations: [
        {
            userid: {
                type: String,
                default: '57c1c0f3b6b20c011242bf22'
            },
            location: {
                type: Number,
                default: 1
            },
            easycrime: [
                {
                    optioname : {
                        type: String,
                        default: 'unknown'
                    },
                    chance: {
                        type: Number,
                        default: 500
                    }
                }
            ],
            heavycrime: [
                {
                    optioname : {
                        type: String,
                        default: 'unknown'
                    },
                    chance: {
                        type: Number,
                        default: 500
                    }
                }
            ],


        }
    ],

    timesteal: {
        type:Number,
        default: 0
    }
like image 364
maria Avatar asked Oct 25 '16 12:10

maria


People also ask

How do you create a multidimensional array?

You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

Which one is the correct way to declare multidimensional array?

Which of the following is the correct way to declare a multidimensional array in Java? Explanation: The syntax to declare multidimensional array in java is either int[][] arr; or int arr[][]; 5.

How do you loop a multidimensional array?

Looping through multidimensional arrays Just as with regular, single-dimensional arrays, you can use foreach to loop through multidimensional arrays. To do this, you need to create nested foreach loops — that is, one loop inside another: The outer loop reads each element in the top-level array.


1 Answers

I presume that easyCrime is Model, cause there is no such thing as findOne query in a Document. If it is a Model please name it EasyCrime.



I had a really hard time interpreting your question. Base on what I understand, this is your solution
EasyCrime
    .findOne({ userid: param.userid})
    .exec((err, crime) => {

        //userid not exists at all, create new
        if (!crime) {
            let newCrime = new EasyCrime({...});
            newCrime.save(...);
            return;
        }

        //Check if location exists
        for (let i = 0; i < crime.locations.length; ++i) {
            if (crime.locations[i].location === param.location) {
                //crime.location[i] is what you're finding
                return;
            }
        }

        //Cannot find any location with provided param.location
        crime.locations.push({
            userid: ...,
            location: param.location,
            ...
        });

        crime.save(...);
    })
like image 155
rocketspacer Avatar answered Oct 02 '22 21:10

rocketspacer