Explanation of mongo query with OR condition - mongodb

I have this data in my db
{ "_id" : ObjectId("5c89da093180684aba34c5b7"), "name" : "Allen", "age" : 24, "nicknames" : [ "kanky" ], "siblings" : [ ] }
{ "_id" : ObjectId("5c89da4b3180684aba34c5b8"), "name" : "Sonata", "age" : "30" }
{ "_id" : ObjectId("5c89da8f3180684aba34c5b9"), "name" : "Kaushik", "age" : "20" }
{ "_id" : ObjectId("5c89da8f3180684aba34c5ba"), "name" : "Stuart", "age" : "24" }
{ "_id" : "5c89da093180684aba34c5b7", "name" : "Allen", "age" : 24 }
When I run this query db.people.find({$or: [{name:"Allen"}, {age:24}]}), it doesn't give the entry with name : Stuart which has age : 24.
But if I run this query db.people.find({$or: [{name:"Stuart"}, {age:24}]}), it works as intended.
Can anyone explain how does this work? I am starting with mongodb, so mightbe a very basic question.
Thanks

You have different schema types for age in different documents. For Allen you are using a number data type for age and for Stuart you are using a string to store age. I think thats the problem.
Try running this:-
db.people.find({$or: [{name:"Allen"}, {age: "24" }]})
You will get Stuart with this query. I don't see anything else wrong here except for different data types.

Related

mongodb lookup giving empty array

BED_MAST this is my one collection bed_mast contains WARD_ID and want to perform join to my other collection with is WARD_MAST given below.
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46376"),
"Bed_id" : "bd-10",
"WARD_ID" : "4",
"OCCUPIED" : "0",
"BED_TYPE" : "single AC"
}
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46377"),
"Bed_id" : "bd-11",
"WARD_ID" : "1",
"OCCUPIED" : "0",
"BED_TYPE" : "single Non AC"
}
WARD_MAST this is my WARD_MAST having ward_id. but while I am putting lookup I am not getting any data.
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46544"),
"patient_id" : null,
"ward_id" : 1,
"total_beds" : 55,
"ward_name" : "Ward 1"
}
{
"_id" : ObjectId("5e53c95d26b0e5ad0fb46545"),
"patient_id" : null,
"ward_id" : 2,
"total_beds" : 63,
"ward_name" : "Ward 2"
}
MY query is
db.BED_MAST.aggregate([{$lookup:{'from':"WARD_MAST",'localField':"WARD_ID",'foreignField':"ward_id",'as':"lookup_value"}}]).pretty()
output: I have confirmed the data by running this query to MySQL there it is working fine
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46388"),
"Bed_id" : "bd-28",
"WARD_ID" : "6",
"OCCUPIED" : "0",
"BED_TYPE" : "NICU",
"lookup_value" : [ ]
}
SAMPLE VALUES DATA IS GIVEN ALL DATA IS NOT POSSIBLE TO GIVE. I know it was asked 1000 times but not able to resolve this question. tried to solve with lookup. but it showing blank space. Is anything I am missing.
The problem is BED_MAST collection's WARD_ID has string values and WARD_MAST collection's ward_id has Number values.

MongoDB - Update a field only if it doesn't exist

I found a question similar to mine regarding updating a document if a certain field does not exist. But in the question, it is not an array of documents like what I have
{
"_id" : ObjectId("5a5f814487c320156094c144"),
"sender_id" : "123",
"iso_number" : "ABC-DEF-123",
"subject" : "Sample Memo",
"content" : "This is a sample memorandum sent through postman.",
"recipients" : [
{
"faculty_number" : 222,
"_id" : ObjectId("5a5f814487c320156094c146"),
"status" : "Sent"
},
{
"faculty_number" : 111,
"_id" : ObjectId("5a5f814487c320156094c145"),
"status" : "Sent"
}
],
"memo_created" : ISODate("2018-01-17T17:00:52.104Z"),
"__v" : 0
}
I'm trying to attach a memo_seen to recipient with faculty_number 111, I'm using the query.
db.getCollection('memos').update({"_id": id, "recipients.faculty_number": faculty_number, "recipients.$.memo_seen": {$exists: false}}, {$set: {"recipients.$.memo_seen": timestamp}})
The timestamp should not update when I repeat the query. Can someone help me out? Been stuck for quite some time now.

$nin operator doesn't work correctly

When I use this method, I get all documents from _User collection. (as expected)
db.getCollection("_User").find({ "_id" : { $nin: [] }})
However, when I add at least one string to $nin array:
db.getCollection("_User").find({ "_id" : { $nin: ["7HpHbmF5iu","ga61t3afsa"] }})
I get empty results. (expected - to get all documents without the ones with ids 7HpHbmF5iu and ga61t3afsa)
MongoDB version 3.2.6
These are two documents I want to filter from all:
{ "_id" : "7HpHbmF5iu", "expiration_date" : ISODate("2015-12-28T08:51:21.252Z"), "_created_at" : ISODate("2015-10-29T08:51:23.302Z"), "_updated_at" : ISODate("2016-03-10T17:37:45.262Z"), "gender" : "female", "firstName" : "Toma", "verification" : "verified", "birthdayDate" : ISODate("1995-02-15T00:00:00Z") }
{ "_id" : "ga61t3afsa", "expiration_date" : ISODate("2015-12-27T07:54:21.235Z"), "_created_at" : ISODate("2015-10-28T07:11:22.102Z"), "_updated_at" : ISODate("2016-03-11T16:11:10.100Z"), "gender" : "female", "firstName" : "Goda", "verification" : "verified", "birthdayDate" : ISODate("1992-09-13T00:00:00Z") }
It was an error in Azure DocumentDB with Mongo, however I've chosen not to use their services, so I don't know if they've already fixed it.

MongoDB count occurances of a substring in a collection

Hello I'm a MongoDb beginner. I have a database of a IRC chatlog. The document structure is very simple
{
"_id" : ObjectId("000"),
"user" : "username",
"message" : "foobar foobar potato idontknow",
"time" : NumberLong(1451775601469)
}
I have thousands of these and I want to count the number of occurrences of the string "foobar". I have googled this issue and found something about aggregations. I looks very complicated and I haven't really found any issue this "simple". I'd be glad if someone pointed me in the right direction what to research and I wouldn't mind an example command that does exactly this what I want. Thank you.
There is no any built-in operator to solve your request.
You can try this query, but it has very poor performance:
db.chat.find().forEach(function(doc){
print(doc["user"] + " > " + ((doc["message"].match(/foobar/g) || []).length))
})
If you could change your message field to array, then we could apply aggregation...
EDIT:
If you add array of splitted words into your entry, we can apply aggregation
Sample:
{
"_id" : ObjectId("569bb7040586bcb40f7d2539"),
"user" : "username",
"fullmessage" : "foobar foobar potato idontknow",
"message" : [
"foobar",
"foobar",
"potato",
"idontknow"
],
"time" : NumberLong(1451775601469)
}
Aggregation. We create new entry for each array element, match given word (foobar, in this case) and then count matched result.
db.chat.aggregate([
{"$unwind" : "$message"},
{"$match" : {"message" : {"$regex" : "foobar", "$options" : "i"}}},
{"$group" : {_id:{"_id" : "$_id", "user" : "$user", "time" : "$time", "fullmessage" : "$fullmessage"}, "count" : {$sum:1}}},
{"$project" : {_id:"$_id._id", "user" : "$_id.user", "time" : "$_id.time", "fullmessage" : "$_id.fullmessage", "count" : "$count"}}
])
Result:
[
{
"_id" : ObjectId("569bb7040586bcb40f7d2539"),
"count" : 2,
"user" : "username",
"time" : NumberLong(1451775601469),
"fullmessage" : "foobar foobar potato idontknow"
}
]

like query in mongoDB

I am working on mongodb . In which i Want to use like query. my collection structure is as follows.
{ "name" : "xy" , "age" : 34 , "location" : "sss"}
{ "name" : "xyx" , "age" : 45 , "location" : "sshs"}
{ "name" : "x" , "age" : 33 , "location" : "shhss"}
{ "name" : "pq" , "age" : 23 , "location" : "hhh"}
{ "name" : "pqr" , "age" : 12 , "location" : "sss"}
i want to find records matching to "name" : "x".
so query will return all three records matching xy ,xyz,x.
Is it possible in mongo.
if any one knows plz reply.
Thanks
You can use regular expressions to do this:
db.customers.find( { name : /^x/i } );
You will probably want to have some indexes on the name field.
Read more at the MongoDB Documetation site.
you can visite
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
You may use regexes in database query expressions!