i have my collection something like this
{
"_id" : ObjectId("58492f1a23e7a2bc20a252ce"),
"type" : "valuation",
"quote" : {
"schedule" : "no",
"valuation" : "XXX",
"designation" : "III",
"companyname" : "XYZ",
"city" : "Delhi",
"country" : "India",
"phone" : "991147****",
"email" : "atul.agrawal#abc.com",
"mobile" : "991147****",
"lname" : "Agrawal",
"fname" : "Atul",
"product" : {
"contactNumberAsAbove" : true,
"contactPersonAsAbove" : true,
"description" : "Test obj",
"mfgYear" : 2009,
"city" : "Delhi",
"model" : "2518",
"brand" : "Ashtt9t",
"category" : "Tipjbih"
}
},
"createdAt" : ISODate("2016-12-08T09:59:54.686Z"),
"__v" : 0
}
{
"_id" : ObjectId("58492f8e23e7a2bc20a252d1"),
"type" : "shipping",
"quote" : {
"comment" : "Hello World",
"packaging" : "no",
"allowed" : "no",
"designation" : "II",
"companyname" : "XYZ",
"city" : "Delhi",
"country" : "India",
"phone" : "99114733**",
"email" : "atul.agrawal#xyz.com",
"mobile" : "99114733**",
"lname" : "Agrawal",
"fname" : "Atul"
},
"createdAt" : ISODate("2016-12-08T10:01:50.001Z"),
"__v" : 0
}
now when i made this query
db.collection.find({$text:{$search:'abc#test.com'}}).pretty()
it gives me all the document in the collection.
i also made the text index on all the fields using this query
db.serviceenquiries.createIndex({"$**":"text"})
how can i search for email id
Give a try to search phrase:
db.collection.find({$text:{$search:"\"abc#test.com\""}}).pretty()
Related
I want my recent order at the top
I am trying
Order.find({ userID: req.params.id })
.sort({'timestamps': -1})
but it dosen't work.
I have added sample code for you which I have done on mongo shell. You can do it same way.
Following is the sample data document with collection name people in the database:
{ "_id" : ObjectId("60540411ad84317971f11dc3"), "name" : "john", "age" : 24, "gender" : "female" }
{ "_id" : ObjectId("60540457ad84317971f11dc4"), "name" : "sarah", "age" : 15, "gender" : "male" }
{ "_id" : ObjectId("60540536ad84317971f11dc5"), "name" : "jimmy", "age" : 19, "gender" : "male" }
{ "_id" : ObjectId("60540583ad84317971f11dc6"), "name" : "sashi", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("6054074fad84317971f11dc7"), "name" : "kevin", "age" : 39, "gender" : "male" }
{ "_id" : ObjectId("60540760ad84317971f11dc8"), "name" : "daisy", "age" : 5, "city" : "WA", "gender" : "female" }
{ "_id" : ObjectId("60540786ad84317971f11dc9"), "name" : "kevin vien", "age" : 25, "gender" : "female" }
{ "_id" : ObjectId("60540791ad84317971f11dca"), "name" : "sara", "age" : 24, "gender" : "male" }
{ "_id" : ObjectId("605554facebf6a49ed5b6e97"), "name" : "chloe", "city" : "LA", "age" : 56, "mothertoungue" : "english", "gender" : "female" }
{ "_id" : ObjectId("60593aec8658728413dd21bf"), "name" : "brett", "age" : 28, "gender" : "female" }
{ "_id" : ObjectId("60593afb8658728413dd21c0"), "name" : "matt", "age" : 28, "gender" : "female" }
{ "_id" : ObjectId("60593b0c8658728413dd21c1"), "name" : "george", "age" : 29, "gender" : "female" }
{ "_id" : ObjectId("60593b1d8658728413dd21c2"), "name" : "cassindra", "age" : 22, "gender" : "male" }
{ "_id" : ObjectId("60593b2a8658728413dd21c3"), "name" : "daisy pies", "age" : 49, "gender" : "male" }
{ "_id" : ObjectId("60593b3e8658728413dd21c4"), "name" : "john dion", "age" : 47, "gender" : "male" }
{ "_id" : ObjectId("605a914f5cc4ed47147fa728"), "name" : "dweyn", "city" : "tokyo", "gender" : "female", "age" : 23 }
So, if you sort for the above then the code would be as below:
db.people.find({'gender':'female'},{name:1,age:1,_id:0,gender:1}).sort({age:-1})
This perfectly work fine for me. It should for you as well.
The data :
{
"_id" : ObjectId("5da6ea7228cc5e07b48173b5"),
"name" : "Joshi",
"email" : "joshiga#gmail.com",
"password" : "4emc8122",
"phone" : "60000000001",
"myAddresses" : [
{
"_id" : ObjectId("5da6eb511e21bf07ce33b868"),
"address" : "Office",
"firstName" : "joshi",
"lastName" : "g",
"country" : "India",
"city" : "VISAKHAPATNAM",
"province" : "Andhra Pradesh",
"postalCode" : 521344,
"phoneNumber" : 8100000006.0,
"address1" : "D.no:12,",
"address2" : "santhi nagar, gurudwara"
},
{
"_id" : ObjectId("5da6ec5e20a5db07da27fbc6"),
"address" : "Home",
"firstName" : "joshi",
"lastName" : "g",
"country" : "India",
"city" : "Vijayawada",
"province" : "Andhra Pradesh",
"postalCode" : 521333,
"phoneNumber" : 8000000006.0,
"address1" : "D.no:11-41,",
"address2" : "main road, kalidindi"
}
],
"__v" : 0
}
You can use the following:
CollectionName.update({'myAddresses._id': "Your ID"}, {'$set': {
'myAddresses.$. address': 'updated Address'
}}, function(err) { ...
ModelName.update({
"_id": req.params.id,
"myAddresses": {
"$elemMatch": {
"_id": req.params.addressId
}
}
}, {
$set: {
"myAddresses.$.address": req.body.address,
"myAddresses.$.firstName": firstName,
//add all fields
}
})
I need a query for:
whose userids are : "userId" : "111b/c", "userId" : "111a". based on the user ids how many are $eq:light_pad:20 with them i need userId:111b/c-max date,userId:111a-max date
DB data
{ "_id" : ObjectId("5a7c1d167826eaca4b4e4398"), "firstName" : "varun", "secName" : "raju", "userId" : "111a", "light_pad" : "20", "lights" : "on", "CREATE_DATE" : ISODate("2018-05-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "76%", "Address" : { "Street" : "Burkit Road", "City" : "chennai", "State" : "Tamil Nadu" }, "Gender" : "Male" }
{ "_id" : ObjectId("5a7c1db77826eaca4b4e4399"), "firstName" : "raju", "secName" : "rani", "userId" : "111a", "light_pad" : "40", "lights" : "off", "CREATE_DATE" : ISODate("2017-04-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "86%", "Address" : { "Street" : "Burkit Road", "City" : "chennai", "State" : "Tamil Nadu" }, "Gender" : "female" }
{ "_id" : ObjectId("5a7c1e707826eaca4b4e439b"), "firstName" : "ranga", "secName" : "mahesh", "userId" : "111a", "light_pad" : "26", "lights" : "on", "CREATE_DATE" : ISODate("2012-04-08T03:12:00Z"), "Department" : "Computer Science and Engg", "Percentage" : "7%", "Address" : { "Street" : "Burkit ", "City" : "vizag", "State" : "Tamil Nadu" }, "Gender" : "female" }
{ "_id" : ObjectId("5a7c1f5d7826eaca4b4e439d"), "firstName" : "kalu", "secName" : "ramana", "userId" : "111b/c", "light_pad" : "25", "lights" : "off", "CREATE_DATE" : ISODate("2014-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "46%", "Address" : { "Street" : "Burkit ", "City" : "bhimavaram", "State" : "Tamil Nadu" }, "Gender" : "male" }
{ "_id" : ObjectId("5a7c1fb37826eaca4b4e439e"), "firstName" : "praveen", "secName" : "pani", "userId" : "111b/c", "light_pad" : "52", "lights" : "on", "CREATE_DATE" : ISODate("2016-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "36%", "Address" : { "Street" : "vin ", "City" : "palakoderu", "State" : "Tamil Nadu" }, "Gender" : "male" }
{ "_id" : ObjectId("5a7c20057826eaca4b4e439f"), "firstName" : "vani", "secName" : "karun", "userId" : "111b/c", "light_pad" : "52", "lights" : "on", "CREATE_DATE" : ISODate("2013-06-08T03:12:00Z"), "Department" : "maths", "Percentage" : "76%", "Address" : { "Street" : "bali ", "City" : "srikakulam", "State" : "Tamil Nadu" }, "Gender" : "male" }
Any answer will be appreciated
Thanks in advance.
I don't understand your question properly, but I have a solution that could be related to your question. I hope it will help you.
db.test.aggregate([{
$match: {
_id: {
$in: [ObjectId("5a7c1d167826eaca4b4e4398"),
ObjectId("5a7c1db77826eaca4b4e4399")]
},
light_pad: {
$eq: "20"
}
}
}]);
It will gives the given result:
{
"_id" : ObjectId("5a7c1d167826eaca4b4e4398"),
"firstName" : "varun",
"secName" : "raju",
"userId" : "111a",
"light_pad" : "20",
"lights" : "on",
"CREATE_DATE" : ISODate("2018-05-08T03:12:00.000Z"),
"Department" : "Computer Science and Engg",
"Percentage" : "76%",
"Address" : {
"Street" : "Burkit Road",
"City" : "chennai",
"State" : "Tamil Nadu"
},
"Gender" : "Male"
}
I need to add one more address like address.2._id,type,etc...:
{
"address" : {
"1" : {
"_id" : ObjectId("5a570f3d422c8223bac22dca"),
"type" : "Primary",
"street" : "58,Jones road",
"area" : "Saidapet",
"city" : "Chennai",
"pincode" : "600015",
"state" : "Tamil nadu",
"country" : "India"
}
}
}
How do I insert a particular document under a nested document?
I have constructed a graph in Arangodb.
I'm struggling to get the below requirement.
Given a node, and i need a list of all the nodes connected to it along with the depth it is connected to.
Example:
Customer2 -> connected to Customer1 at depth of 1,
Customer3 -> Connected to Customer1 at depth of 2 and so on..
Please help me in achieving this.
Assuming you're using the pattern matching traversals this is easy achieveable.
Lets try this using the Knows Graph example starting the traversal at Eve:
db._query(`FOR v, e IN 1..3 OUTBOUND 'persons/eve'
GRAPH 'knows_graph'
RETURN {v: v, e: e}`)
[
{
"e" : {
"_from" : "persons/eve",
"_id" : "knows/156",
"_key" : "156",
"_rev" : "156",
"_to" : "persons/alice"
},
"v" : {
"_id" : "persons/alice",
"_key" : "alice",
"_rev" : "130",
"name" : "Alice"
}
},
{
"e" : {
"_from" : "persons/alice",
"_id" : "knows/146",
"_key" : "146",
"_rev" : "146",
"_to" : "persons/bob"
},
"v" : {
"_id" : "persons/bob",
"_key" : "bob",
"_rev" : "134",
"name" : "Bob"
}
},
{
"e" : {
"_from" : "persons/bob",
"_id" : "knows/150",
"_key" : "150",
"_rev" : "150",
"_to" : "persons/charlie"
},
"v" : {
"_id" : "persons/charlie",
"_key" : "charlie",
"_rev" : "137",
"name" : "Charlie"
}
},
{
"e" : {
"_from" : "persons/bob",
"_id" : "knows/153",
"_key" : "153",
"_rev" : "153",
"_to" : "persons/dave"
},
"v" : {
"_id" : "persons/dave",
"_key" : "dave",
"_rev" : "140",
"name" : "Dave"
}
},
{
"e" : {
"_from" : "persons/eve",
"_id" : "knows/159",
"_key" : "159",
"_rev" : "159",
"_to" : "persons/bob"
},
"v" : {
"_id" : "persons/bob",
"_key" : "bob",
"_rev" : "134",
"name" : "Bob"
}
},
{
"e" : {
"_from" : "persons/bob",
"_id" : "knows/150",
"_key" : "150",
"_rev" : "150",
"_to" : "persons/charlie"
},
"v" : {
"_id" : "persons/charlie",
"_key" : "charlie",
"_rev" : "137",
"name" : "Charlie"
}
},
{
"e" : {
"_from" : "persons/bob",
"_id" : "knows/153",
"_key" : "153",
"_rev" : "153",
"_to" : "persons/dave"
},
"v" : {
"_id" : "persons/dave",
"_key" : "dave",
"_rev" : "140",
"name" : "Dave"
}
}
]
Each step in the traversal will map to one Object in the list. You can also connect the vertices (persons) with the _from and _to attributes of the edges (knows)
Only looking at the paths is also possible; you can use the path attribute:
db._query(`FOR v, e, p IN 2..2 OUTBOUND 'persons/eve'
GRAPH 'knows_graph'
RETURN {p: p}`)
We only return the paths for the end point of the iteration, limiting it to 2 to be a little better overviewable; Here is one of the resulting paths:
[
...
{
"p" : {
"edges" : [
{
"_from" : "persons/eve",
"_id" : "knows/159",
"_key" : "159",
"_rev" : "159",
"_to" : "persons/bob"
},
{
"_from" : "persons/bob",
"_id" : "knows/153",
"_key" : "153",
"_rev" : "153",
"_to" : "persons/dave"
}
],
"vertices" : [
{
"_id" : "persons/eve",
"_key" : "eve",
"_rev" : "143",
"name" : "Eve"
},
{
"_id" : "persons/bob",
"_key" : "bob",
"_rev" : "134",
"name" : "Bob"
},
{
"_id" : "persons/dave",
"_key" : "dave",
"_rev" : "140",
"name" : "Dave"
}
]
}
}
]