like query in mongoDB - 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!

Related

mongodb update/upsert that adds data to the existing document instead of replacing data in an existing document

I am using an upsert using mongodb's Jensseger PHP library. Every time I add new data to the document it updates the value of the field if the field names are the same. I want to add to the document's data instead of replacing it.
An example of a record in my session_activities collection:
"_id" : ObjectId("622f8da565fbcea0a1b4ed12"),
"activity" : [
{
"name" : "Fullscreen Off",
"time" : 219.972769,
"value" : 0
},
{
"name" : "Player Resize",
"time" : 220.074389,
"value" : 354
},
{
"name" : "close",
"time" : 223.779885,
"value" : 369
}
]
If that record has a new "Player Resize" event at 500 seconds I want the document to change to:
"_id" : ObjectId("622f8da565fbcea0a1b4ed12"),
"activity" : [
{
"name" : "Fullscreen Off",
"time" : 219.972769,
"value" : 0
},
{
"name" : "Player Resize",
"time" : 220.074389,
"value" : 354
},{
"name" : "Player Resize",
"time" : 500,
"value" : 354
},
{
"name" : "close",
"time" : 223.779885,
"value" : 369
}
]
Right now the Player Resize field is being overwritten with the new value. My mongo statement looks like the below:
(new VideoAnalytics)->where($whereStatement)->update($this->newobj, ['upsert' => true])
If you give the entire object with the key values you provided into the MongoDB update method it will replace the whole record, instead of that please start using $set in MongoDB to set the specific keys. Here also I'm adding a reference link for better understanding.
https://php.uz/manual/en/mongocollection.update.php
new VideoAnalytics->update(
$whereStatement,
array('$set' => $this->newobj),
array('upsert' => true)
);
as I understand, you wanna update by pushing a new element to the array, right?
this might help push new value to mongodb inner array - mongodb/php

Explanation of mongo query with OR condition

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.

Getting data range from firebase with swift

I would like to ask that question. The question is how to get specific data range from firebase ?
I have table on firebase like this:
"users" : {
"Jz3IpatRWiWoDbiYM62q6qbHB503" : {
"email" : "Kaanozdemir#gmail.com",
"lastName" : "Ozdemir",
"name" : "Kaan"
},
"PmeYYFiac0c55fU2sFpnTP308mC3" : {
"email" : "kevinhart#gmail.com",
"lastName" : "Hart",
"name" : "Kevin"
},
"r0bMqSGCWihFi2EF4u6ckSzLP8v1" : {
"email" : "Marcusalvarez#gmail.com",
"lastName" : "Alvarez",
"name" : "Marcus"
},
"A3tmSSGCWihFi2EF4u6ckSzLP8c1" : {
"email" : "taylorswift#gmail.com",
"lastName" : "Swift",
"name" : "Taylor"
},
"3SUTsiGCWihFi2EF4u6ckSzLP8v2" : {
"email" : "jimmyfellon#gmail.com",
"lastName" : "Fellon",
"name" : "Jimmy"
},
"lgSit3GCWihFi2EF4u6ckSzLP8u3" : {
"email" : "jaxteller#gmail.com",
"lastName" : "Teller",
"name" : "Jax"
}
For example, I would like to get users values between 2 and 4 [2 - 4](Marcus Alvarez - Taylor Swift - Jimmy Fellon).
Is there any way to do that server side ? I don't wanna get all data and pick values that I want. Anyone knows?
Change your JSON DB structure to include an index in every node :
"users" : {
"autoID1" : {
"email" :.....,
"lastName" : ......,
"name" :.......,
"index" : //e.g.. 1,2,3,4......
}
},
"noOfUsers" : 223,
If you are appending this users node via app, you have too keep track of the no of users in Database node users and keep updating the noOfUsers whenever a new user is added. And to set the next ones index number , just retrieve that node value i.e 223 and sees it and then increment the noOfUsers......
To retrieve between 2-4 .. Now you can use :
Database.database().reference().child("users").queryOrdered(byChild: "index").queryStarting(atValue: "2").queryEnding(atValue: "4").observe....

Check if particular field exists in mongoDb Collection,excluding one record

I have collection, as shown below
{
"_id" : ObjectId("58fe3768f997ca09d551c34e"),
"firstName" : "arbar",
"lastName" : "ame",
"email" : "test41#gmail.com",
"phone" : "9966589965",
"password" : "$2a$10$pgRWb8Db385A5BbicEDJ2erHuUQsAIVmjqVuccXj7x.1iptdY/z7a",
"team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"),
"activated" : false,
"accessLevel" : NumberInt(6)
}
{
"_id" : ObjectId("58fe37c2f997ca09d551c350"),
"firstName" : "Abrar Ahmed",
"lastName" : "asdf",
"email" : "test42#gmail.com",
"phone" : "9966158845",
"password" : "$2a$10$y3hPjuHeq0HVyukTnGCRT.k5xfSUH0z/mdGR8n7Gu09f7A7Z20bV6",
"team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"),
"activated" : false,
"accessLevel" : NumberInt(6)
}
.
.
.
.
.
.
I am trying to check if email test41#gmail.com exists in this collection, if I use this.collection.findOne({ email: 'test41#gmail.com' }); will look for all the records in a collection but how would I exclude one record in a collection by using _id field as reference to exclude.
Here the email is exists in one record of the collection with
"_id" : ObjectId("58fe3768f997ca09d551c34e"), , but I want to exclude this record and search in rest of the records in a Collection.
Don't use findOne, use find to find all occurrences. Now if you want to exclude some document you can do by following
db.collection.find({$and: [{"email": "test41#gmail.com"}}, {"_id": {$ne: ObjectId("58fe3768f997ca09d551c34e")}}]})

geo-spatial query with sort and limit on a secondary field

I am trying to obtain the most recently created 3 accounts in the nearby area.
So I tried to use a geo-spatial query combined with a sort(created_at: -1) and limit(3)
The basic geo-spatial query:
db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}})
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-05T12:01:34.199Z"), "name" : "D" }
{ "created_at" : ISODate("2014-03-13T08:16:22.913Z"), "name" : "E" }
{ "created_at" : ISODate("2014-03-13T10:23:02.660Z"), "name" : "F" }
Adding sorting to it gave:
db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}}).sort({created_at: -1})
{ "created_at" : ISODate("2014-03-13T10:23:02.660Z"), "name" : "F" }
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-13T08:16:22.913Z"), "name" : "E" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2014-03-05T12:01:34.199Z"), "name" : "D" }
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }
And finally adding a limit gave:
db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}}).sort({created_at: -1}).limit(3)
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }
The expected result was [F,C,E] (i.e the first 3 entries from the second query-result). But instead I get [C,B,A], which is the first 3 entries from the first query-result (sorted by creation time).
So mongodb is performing the limit operation before it performs the sort operation. Is there any way to force it to sort before applying the limit ?
A similar question was asked here, but there was an issue in the query itself.
Is this issue specific to '$near' queries ?
The geo-spatial query already sorts the documents from nearest to farthest. So, if you have an additional sort() and limit(), limit() is applied first since the results are already sorted by distance.
Although I think it doesn't make much sense to sort by "created_at" in a geo-spatial query, if you still need it, it can only be done programmatically. That is, sort the entire results in the query and apply the limit in your client program.