Removing an element from array in mongodb - node.js

I want to remove a specific element from array stored in mongodb document.
I am using this:
model.registerCompany.findOneAndUpdate({companyKey:"a key"},
{$pop:{onlineEmployees:"John"}},
function(err,doc){
if(!err)
console.log("Online list modified: ",doc);
else
console.log("Online list modified error :",err);
});
But I am not sure if the $pop removes the specific element "John" from array(onlineEmployees) or just pop out the last element from it.
Am i doing it right or there is another way to do it.?
I think i got the answer .. $pull is used for this purpose as explained here in the link:
http://docs.mongodb.org/manual/reference/operator/pull/#_S_pull

The $pop operator will remove first or last element of the array, which may not necessarily be the right one.
If you want a specific element, you can $pull the item with defined criteria:
model.registerCompany.findOneAndUpdate({companyKey:"a key"},
{$pull:{onlineEmployees:"John"}},
You have to make sure the value in the array is unique, for $pull removes every element matching the name 'John'.
If identical values exist in the array, you need to use $unset and $ positional operator to set the target element value to null(unfortunately $unset won't remove elements) and then use $pull to remove the element with null value. To do that, you have to make sure valid value can not be null. In that case, the code could be like:
model.registerCompany.findOneAndUpdate({companyKey:"a key", onlineEmployees:"John"},{ $unset: { "onlineEmployees.$" : '' } } )
model.registerCompany.findOneAndUpdate({companyKey:"a key"},{ $pull: { "onlineEmployees" : null } } )

Related

get list of collections having all words exists in field which added in given string in mongoDB

I want to search list of collections from mongoDB have all the keywords of given string.
For e.g.
I have a collection
{
"id":1
"text":"go for shopping",
"description":"you can visit this branch as well"
}
{
"id":2
"text":"check exiting discount",
"description":"We have various discount options"
}
Now, If I will pass string like "I want to go for shopping" w.r.t. text field in find query of mongoDB. Then I should get first collection as output because text field value "go for shopping" exists in the input string passed in find query.
This can be achieved through $text operator in MongoDB. But you have to createIndex on the "text" field in your database.(or whichever filed you want to be matched, I would suggest you rename it in your db to avoid confusion)
db.yourCollectionName.createIndex({"text":"text"})
The first field here is the "text" field in your database, and the second one is the mongo operator.
Then you can pass any query like,
db.yourCollectionName.find({$text: {$search: "I want to go for shopping"}})
The "$text" here is the mongo operator.
This would return all documents which have any of the keywords above.
Maybe you can read more around this and improvise and modify.
Ref: MongoDb $text
You can do so through regular expression. MongoDb provides the provision of matching strings through regex patterns.
In your case you could do something like:
db.yourCollectionName.find({text:{$regex:"go for shopping" }})
This will return you all the documents having the phrase "go for shopping" in the text field.
Ref: MongoDb Regex

Elasticsearch: How to get the length of a string field(before analysis)?

My index has a string field containing a variable length random id. Obviously it shouldn't be analysed.
But I don't know much about elasticsearch especially when I created the index.
Today I tried a lot to filter documents based on the length of id, finally I got this groovy script:
doc['myfield'].values.size()
or
doc['myfield'].value.size()
both returns mysterious numbers, I think that's because of the field got analysed.
If it's really the case, is there any way to get the original length or fix the problem, without rebuild the whole index?
Use _source instead of doc. That's using the source of the document, meaning the initial indexed text:
_source['myfield'].value.size()
If possible, try to re-index the documents to:
use doc[field] on a not-analyzed version of that field
even better, find out the size of the field before you index the document and consider adding its size as a regular field in the document itself
Elasticsearch stores a string as tokenized in the data structure ( Field data cache )where we have script access to.
So assuming that your field is not not_analyzed , doc['field'].values will look like this
"In america" => [ "in" , "america" ]
Hence what you get from doc['field'].values is a array and not a string.
Now the story doesn't change even if you have a single token or have the field as not_analyzed.
"america" => [ "america" ]
Now to see the size of the first token , you can use the following request
{
"script_fields": {
"test1": {
"script": "doc['field'].values[0].size()"
}
}
}

How to get all matches with MongoDB $elemMatch?

I have two objects:
{
genre: ['music', 'movie']
}
and
{
genre: ['movie', 'music']
}
and my query is:
db.test.find({genre :{ $elemMatch:{ $in : ['movie']}}})
and it only gives me the second object. Why? I want to get all the docs that contain a specific genre in their arrays no matter where in the array. How can I do this?
You need neither $elemMatch nor $in in this case. A simple field:value will match documents where field is an array and any one of the values in that array is value. That means
db.test.find({genre :'movie'});
will suffice.
The array query operators are required in more complex situations.
$in is needed when you have a list of possible values and want documents where any of them is found (so db.test.find({genre : { $in:['movie']} }); would work, but would be needlessly convoluted)
$all works like $in but requires that all provided elements are in the array
$elemMatch is a bit more complex. It is required when you want to use multiple operator-conditions (like $gt or $lt) but want only those documents where one array entry matches all the conditions. Without the $elemMatch operator, you get results where each condition is met by at least one array entry, but not necessarily all by the same entry.
Remember db.test.find() in general returns you cursor object. You can access all doc as follows:
entry = db.test.find({genre :{ $elemMatch:{ $in : ['movie']}}})
for doc in entry:
print(doc)

Getting the property of an associative array given as an escaped string

I have an array that looks like this:
[{"x": "someValue",
"y" : "{\"iWantThisValue\":\"a\", \"otherVal2\":\"b\"}"}]
I want to get the value of "iWantThisValue". On the view, I have #{JSON.parse(myArray.y)}. If I try to put something like .iWantThisValue after it, nothing is printed out. Is this a correct step to getting it? Where do I go from here?
You're pretty close, but you need to subscript the array.
var myArray = [
{
"x": "someValue",
"y" : "{\"iWantThisValue\":\"a\", \"otherVal2\":\"b\"}"
}
];
console.log( JSON.parse( myArray[0].y ).iWantThisValue );
Logs a.
Of course in real code, you probably wouldn't just be doing [0] to access an array element, but would probably be looping through the array? Either way, you'd still need to use myArray[index] where index is 0 in this example.
Also, in the interest of clarity, JavaScript does not have anything called an "associative array". It has arrays and it has objects. In your example, myArray is an array of one element. That element is an object which has x and y properties.

Custom MongoDB search query

In my database I have documents which all contain the property foo. For each value of foo I have a function that either returns true or false. How can I query for all the documents for which the value of foo makes the function return true?
If you need to check if your string field's value is one of several, you need the $in modifier.
db.collection.find( { field : { $in : array } } );
It works fast and uses index (if possible).
If your field is an array and you pass a string, use this syntax.
db.collection.find({array_field : string_value});
It will check every element in the array and, if any of them matches your string, it will return the document.
You could use $where.
Example:
db.myCollection.find( { $where: "this.a > 3" });
db.myCollection.find( "this.a > 3" );
db.myCollection.find( { $where: function() { return this.a > 3;}});
Note, this is run in Javascript. This means two things.
You can put arbitrary Javacript into $where expression (the function form).
It'll be significantly slower than regular queries.
It really depends on what the function is and how you are using it. Is the function constant for any given record? Is it even a function you can evaluate on the database server? ...
In the extreme, if you need to check this value often, you might, for example, create a field that exists only when f(foo) is true and then create a sparse index on that field.
$where may well be the solution you are looking for, but depending on the access patterns there may be a better solution.

Resources