How to User not operator In MongoTemplate - mongodb

I have been troubled By this question much time. There has a collection which has a phone_num column,
how can I query the document except the column value is not 12563254154

Use the $ne operator
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24ne
So for example
db.things.find( { phone_num : { $ne : 12563254154 } } );

Related

$elemMatch and $eq equivalent in spring data mongodb

I have a following query for my mongodb. How to translate it to the equivalent code in spring data mongodb:
db.getCollection('account').find({
colorList: {$elemMatch: {
$eq:"577b"
}
}
})
one of the account collection is shown as below:
{
"_id" : ObjectId("133b6ca05e7c058819ab6e6c"),
"fleetList" : [
"577b",
"123b"
]
}
Instead of using $elemMatch and $eq, you can use $in for your query too. This query makes exactly that your query does:
db.account.find({ "colorList": { $in: ["577b"] } });
And for spring-data-mongodb method for this query is:
List<Account> findByColorListIn(List<String> colorIds); //In your case colorIds list has one element only.
If you want to stick with your query:
#Query("{'colorList': {\$elemMatch: {\$eq: ?0}}}")
List<Account> findByColorList(String colorId)

MongoDB: Search in array

I have a field in a MongoDB Collection like:
{
place = ['London','Paris','New York']
}
I need a query that will return only that particular entry of the array, where a specific character occurs. For Example, I want to search for the terms having the letter 'o'(case-insensitive) in them. It should just return 'London' and 'New York'.
I tried db.cities.find({"place":/o/i}), but it returns the whole array.
You'll need to $unwind using an aggregate query, then match.
db.cities.aggregate([ { $unwind:'$place' }, { $match: { place : {$regex: /o/i } } } ])
In simple you find with $regex below query will work without aggregation
db.collectionName.find({ place : {$regex: /o/i } })

how to remove all documents from a collection except one in MongoDB

Is there any way to remove all the documents except one from a collection based on condition.
I am using MongoDB version 2.4.9
You can do this in below way,
db.inventory.remove( { type : "food" } )
Above query will remove documents with type equals to "food"
To remove document that not matches condition you can do,
db.inventory.remove( { type : { $ne: "food" } } )
or
db.inventory.remove( { type : { $nin: ["Apple", "Mango"] } } )
Check here for more info.
To remove all documents except one, we can use the query operator $nin (not in) over a specified array containing the values related to the documents that we want to keep.
db.collections.remove({"field_name":{$nin:["valueX"]}})
The advantage of $nin array is that we can use it to delete all documents except one or two or even many other documents.
To delete all documents except two:
db.collections.remove({"field_name":{$nin:["valueX", "valueY"]}})
To delete all documents except three:
db.collections.remove({"field_name":{$nin:["valueX", "valueY", "valueZ"]}})
Query
db.collection.remove({ "fieldName" : { $ne : "value"}})
As stated above by Taha EL BOUFFI, the following worked for me.
db.collection.remove({"fieldName" : { $nin: ["value"]}});

How can I use a $elemMatch on first level array?

Consider the following document:
{
"_id" : "ID_01",
"code" : ["001", "002", "003"],
"Others" : "544554"
}
I went through this MongoDB doc for elemmatch-query & elemmatch-projection, but not able to figure it out how to use the same for the above document.
Could anyone tell me how can I use $elemMatch for the field code?
You'll want to use the $in operator rather than $elemMatch in this case as $in can be used to search for a value (or values) inside a specific field. $in requires a list of values to be passed as an array. Additionally, and for your case, it will find either a single value, or by searching in an array of values. The entire matching document is returned.
For example, you might use it like this:
db.mycodes.find( { code: { $in: ["001"] } } )
Which could be simplified to just be:
db.mycodes.find({ code: "001" })
As MongoDB will look in an array for a single match like above ("001").
Or if you want to search for "001" or "002":
db.mycodes.find( { code: { $in: ["001", "002"] } } )
$in documentation
If you're simply looking to match all documents with an array containing a given value, you can just specify the value on the reference to that array, e.g.
db.mycodes.find( { code: '001' } )
Which thus would return you all documents that contained '001' in their code array

mongodb: non-performant indexes

After playing with
db.largecollection.find( { $or : [ { identifierX : "sha1_hash123" } , { identifierY : "md5_hash456" } , { identifierZ : "another_hash789" } ] } )
I checked the indexes that mongodb prepared automatically. in addition to the "single" ensureIndex for the identifiers x/y/z, there is a identifierX_1_identifierY_1_identifierZ_1 now and performance is down :-(
Do you have an idea or tip how to explain to mongodb that it's faster to use the indexes for the single identifiers because i do not have $and, but $or queries?
Thx
MongoDB doesn't create indexes on its own. It's something that an application, user, or framework does. For your query, MongoDB could only use an index for either of identifierX, identifierY or identifierZ. However, if you don't have such an index then of course none will be used. The identifierX_1_identifierY_1_identifierZ_1 index can not be used for this query.
In this case, you will probably need to make an index for all of this identifiers:
db.ensureIndex( { 'identifierX' : 1 } );
db.ensureIndex( { 'identifierY' : 1 } );
db.ensureIndex( { 'identifierZ' : 1 } );
MongoDB can only use one index at a time, and it will try to pick the "best" one. Try using explain to see which indexed is being picked:
db.largecollection.find( { $or : [
{ identifierX : "sha1_hash123" },
{ identifierY : "md5_hash456" },
{ identifierZ : "another_hash789" }
] } ).explain();
That should give you some ideas on which index is being used.
There is an exception for $or though, where MongoDB can use a different index for each of the parts and de-dup for you. It's here in the docs. It would (of course) still not use the compound index, and you need the indexes that I've written here above.