$or query in mongodb not working - mongodb

I already have this document at the db:
> db.test.find()
{ "_id" : ObjectId("4fd349242b153bfbd95a15a8"), "nombre" : "Javier", "apellido" : "Roger" }
Now I execute this query:
db.test.find({"nombre": "Javier"})
{ "_id" : ObjectId("4fd349242b153bfbd95a15a8"), "nombre" : "Javier", "apellido" : "Roger" }
It works as spected.
But when I execute this query, mongodb is not returning any results:
db.test.find({$or:[{"nombre": "Javier"}, {"apellido": "Javier"}]})

When I insert that document your syntax works for me.
$or was new in MongoDB v1.6. Is it possible you're running a really old version?

Related

Updating document with object field

I am new to MongoDB, learning from its documentation but can't find a way to update the document field using MongoDB shell
When I tried to insert the "cancellation" object with either field the query works fine.
db.inventory.update({},
{
$set : { "cancellation":
{
{"date" : new Timestamp()},
{"reason" : "null"}
}
}
},
{upsert : false,multi : true})
It shows
"SyntaxError: invalid property id #(shell)"
on executing this query
Your query is wrong. Try this:
db.inventory.update({},{$set : { "cancellation":
{
"date" : new Timestamp(),
"reason" : "null"
}
} },{upsert : false,multi : true})

mongoDB TypeError when trying to search in database

I have a big database with many documents in one collection. When i use findOne(). It gives me this
> db.sem.findOne()
{
"_id" : ObjectId("56619c6852a9c022d077400b"),
"name" : "MANAN VIJAY",
"reg_no" : "11011",
"dept" : "B.Tech",
"subjects" : {
},
"sem" : "5"
}
now when I try
> db.cse_sem_5.find()(
... {"name": "MANAN VIJAY"}
... )
2015-12-08T14:24:21.052+0530 E QUERY TypeError: object is not a function
at (shell):1:20
Why am I getting this error? And how can I resolve it?
This did not happen when I tried something with a smaller database..
Your find() query looks wrong, try like this:
db.cse_sem_5.find({"name": "MANAN VIJAY"})

Mongodb findOne doesn't work

I'm using for the first time a Mongodb and I got a very weird bug.. I have a 'games' collection and I can't make a search with _id query on it..
I try directly on the mongo shell and this is the result :
> db.games.count()
0
> db.games.insert({created:'ok'})
WriteResult({ "nInserted" : 1 })
> db.games.find()
{ "_id" : ObjectId("54f7364d1f2f9378d7a5ddde"), "created" : "ok" }
> db.games.findOne({_id:'54f7364d1f2f9378d7a5ddde'})
null
> db.games.find({_id:'54f7364d1f2f9378d7a5ddde'})
>
I really don't know what is going on ? I was thinking about a weird index on _id but I found nothing..
> db.games.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "cobra.games"
}
]
>
This can maybe help you
> db.version()
2.6.5
Someone have an idea ?
try:
db.games.find({"_id" : ObjectId("54f7364d1f2f9378d7a5ddde")})
The type of the data stored in the key _id is of ObjectId and your were searching for a String. So you the types were not matching. MongoDB has not auto-casting of String to ObjectId. This Error can also happen if your store your numbers as a Long but in your query you enter an Integer.

Mongo: Import results of an aggregate query?

Sorry for the basic question, I'm new to mongo and learning my way around.
I have run an aggregate query in Mongo:
> var result = db.urls.aggregate({$group : {_id : "$pagePath"} });
> result
{ "_id" : "/section1/page1" }
{ "_id" : "/section1/page2" }
...
Type "it" for more
I would now like to save the results of this aggregate query into a new collection. This is what I've tried:
> db.agg1.insert(result);
WriteResult({ "nInserted" : 1 })
But this seems to have inserted all the rows as just one row:
> db.agg1.count()
1
> db.agg1.findOne();
{ "_id" : "/section1/page1" }
{ "_id" : "/section1/page2" }
...
Type "it" for more
How can I insert these as separate rows?
I've tried inserting the _id directly, without success:
> db.agg1.insert(result._id);
2014-12-17T15:23:26.679+0000 no object passed to insert! at src/mongo/shell/collection.js:196
Use the $out pipeline operator for that:
db.urls.aggregate([
{$group : {_id : "$pagePath"} },
{$out: "agg1"}
]);
Note that $out was added in MongoDB 2.6.

$elemMatch differences in mongo

I'm seeing some differences between 2.0.7 and 2.2.0 when it comes to the $elemMatch operation.
In 2.2.0, I do get results back with this query:
db.testColl.find( { "metadata" : {$elemMatch : {$gt : {age:23}, $lt : {age:99}} }});
In 2.0.7, I don't get any results back.
For testing purposes, I have only one document in my testColl collection:
{
"_id" : ObjectId("4fb2974cbedb4a626109b002"),
"metadata" : [
{
"age" : 59
},
{
"gender" : "FEMALE"
}
]
}
Does anyone know why this works in 2.2.0, but not 2.0.7?
According to this:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24elemMatch
elemMatch is supported for v1.4+
Thanks,
Galen
If you're looking for a way that works in both versions, you don't need to use $elemMatch here because you're only comparing against a single field so you can use a simpler query. Try this instead:
db.testColl.find({ 'metadata.age': { $gt: 23, $lt: 99 }});