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"
}
Related
I have a User schema that looks like this:
{
"_id" : ObjectId("5f0cbcdf8363cd155c2570f5"),
"clients" : [
{
"clientName" : "Chabad of Closter",
"activeInvoice" : [
{
"clientName" : "Chabad of Closter",
"startTime" : "2020-07-16T03:06:19.102Z",
"endTime" : "2020-07-16T06:06:19.102Z",
"duration" : "3.00",
"rate" : "25",
"description" : "email",
"total" : "$75.00"
},
{
"clientName" : "Chabad of Closter",
"startTime" : "2020-07-20T18:49:45.300Z",
"endTime" : "2020-07-20T20:49:45.300Z",
"duration" : "2.00",
"rate" : "25",
"description" : "janitor",
"total" : "$50.00"
},
{
"clientName" : "Chabad of Closter",
"startTime" : "2020-07-20T08:02:04.795Z",
"endTime" : "2020-07-20T21:02:04.795Z",
"duration" : "13.00",
"rate" : "35",
"description" : "something",
"total" : "$455.00"
}
],
"pastInvoices" : []
},
{
"clientName" : "Chabad UC",
"activeInvoice" : [
{
"clientName" : "Chabad UC",
"startTime" : "2020-07-14T00:43:30.689Z",
"endTime" : "2020-07-14T03:43:30.689Z",
"duration" : "3.00",
"rate" : "25",
"description" : "website",
"total" : "$75.00"
},
{
"clientName" : "Chabad UC",
"startTime" : "2020-07-14T00:43:30.689Z",
"endTime" : "2020-07-14T03:43:30.689Z",
"duration" : "3.00",
"rate" : "25",
"description" : "website",
"total" : "$75.00"
},
{
"clientName" : "Chabad UC",
"startTime" : "2020-07-16T08:32:44.980Z",
"endTime" : "2020-07-16T08:32:44.980Z",
"duration" : "0.00",
"rate" : "25",
"description" : "something",
"total" : "$0.00"
},
{
"clientName" : "Chabad UC",
"startTime" : "2020-07-20T20:22:27.399Z",
"endTime" : "2020-07-20T20:22:27.399Z",
"duration" : "0.00",
"rate" : 0,
"description" : "",
"total" : "$0.00"
}
],
"pastInvoices" : []
},
{
"clientName" : "Chabad Mobile",
"activeInvoice" : [
{
"clientName" : "Chabad Mobile",
"startTime" : "2020-07-14T04:05:18.084Z",
"endTime" : "2020-07-14T06:05:18.084Z",
"duration" : "2.00",
"rate" : "25",
"description" : "Fema grant",
"total" : "$50.00"
},
{
"clientName" : "Chabad UC",
"startTime" : "2020-07-16T04:56:51.451Z",
"endTime" : "2020-07-16T05:56:51.451Z",
"duration" : "1.00",
"rate" : "25",
"description" : "grants",
"total" : "$25.00"
},
{
"clientName" : "Chabad of Closter",
"startTime" : "2020-07-16T03:56:51.451Z",
"endTime" : "2020-07-16T05:56:51.451Z",
"duration" : "2.00",
"rate" : "26",
"description" : "grants",
"total" : "$52.00"
},
{
"clientName" : "Chabad Mobile",
"startTime" : "2020-07-15T18:51:00.000Z",
"endTime" : "2020-07-16T08:09:20.001Z",
"duration" : "13.31",
"rate" : "22",
"description" : "website",
"total" : "$292.82"
}
],
"pastInvoices" : []
}
],
"username" : "test50#test.com",
"password" : "************************************",
"firstName" : "John",
"__v" : 0
}
I'm trying to query the "clients" array with a value from the "clientName" property.
app.get("/closeinvoice", function(req, res){
// gets the id for the document
const id = req.user._id;
// gets the value of "clientName" for the query
const c = req.query.client;
User.find({_id: id, "clients.clientName": c}, function(err, foundDoc) {
if (err) {
console.log(err)
} else {
console.log(foundDoc);
}
})
})
Each time I do this, MongoDB returns the entire document. I need it to return only one object from the nested array. I'm also wondering if I could be using a different method from the Mongoose API. I would prefer to use "findByID" but I'm not sure if you can for this type of query.
You can take advantage of the $ (projection) operator which returns the first matching element from an array (based on corresponding `$elemMatch):
User.find({ _id: id, clients: { $elemMatch: { clientName: c }}}, { "clients.$": 1}, function(err, foundDoc) { ... })
Mongo Playground
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 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()
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"
}
]
}
}
]
I have a collection of properties that all have addresses.
"address" : {
"street" : "5 Orange Drive",
"city" : "Orlando",
"state" : {
"abbreviation" : "FL",
"name" : "Florida"
},
"zip" : "32822",
"geo" : {
"lat" : 28.519,
"lng" : -81.304
}
},
"address" : {
"street" : "16 Main Street",
"city" : "Tallahassee",
"state" : {
"abbreviation" : "FL",
"name" : "Florida"
},
"zip" : "32823",
"geo" : {
"lat" : 28.529,
"lng" : -81.314
}
},
"address" : {
"street" : "125 Oak Drive",
"city" : "Salem",
"state" : {
"abbreviation" : "MA",
"name" : "Massachusetts"
},
"zip" : "02108",
"geo" : {
"lat" : 24.519,
"lng" : -83.304
}
},
"address" : {
"street" : "96 Jones Way",
"city" : "Springfield",
"state" : {
"abbreviation" : "MA",
"name" : "Massachusetts"
},
"zip" : "01020",
"geo" : {
"lat" : 28.519,
"lng" : -84.304
}
},
"address" : {
"street" : "100 Sumner Ave",
"city" : "Springfield",
"state" : {
"abbreviation" : "IL",
"name" : "Illinois"
},
"zip" : "32822",
"geo" : {
"lat" : 22.519,
"lng" : -71.304
}
},
"address" : {
"street" : "40 Roger Ave",
"city" : "Salem",
"state" : {
"abbreviation" : "AL",
"name" : "Alabama"
},
"zip" : "32822",
"geo" : {
"lat" : 22.519,
"lng" : -71.304
}
}
I have an earlier query that returns an array of addresses such as:
[
{
name: 'Massachusetts - Salem',
city: 'Salem',
_id: 53784206cd73fbae193b62d5,
state: [Object]
}, {
name: 'Illinois - Springfield',
city: 'Springfield',
_id: 5376fa92bde0e0ea047e9abd,
state: [Object]
}
]
I would like to use the output of the above array to search the collection of properties by address.
Such as, I would like to search for all properties where the address contains a 'city of Springfield and a state of Illinois' and 'city of Salem and a state of Massachusetts'.
I was originally trying to do this using $in with some $and's mixed in but I wasn't sure if it was possible.
You could try using the $or operator on an array that contains query conditions derived from the other input array. For example, with the sample properties collection:
db.properties.insert([
{
"address" : {
"street" : "5 Orange Drive",
"city" : "Orlando",
"state" : {
"abbreviation" : "FL",
"name" : "Florida"
},
"zip" : "32822",
"geo" : {
"lat" : 28.519,
"lng" : -81.304
}
}
},
{
"address" : {
"street" : "16 Main Street",
"city" : "Tallahassee",
"state" : {
"abbreviation" : "FL",
"name" : "Florida"
},
"zip" : "32823",
"geo" : {
"lat" : 28.529,
"lng" : -81.314
}
}
},
{
"address" : {
"street" : "125 Oak Drive",
"city" : "Salem",
"state" : {
"abbreviation" : "MA",
"name" : "Massachusetts"
},
"zip" : "02108",
"geo" : {
"lat" : 24.519,
"lng" : -83.304
}
}
},
{
"address" : {
"street" : "96 Jones Way",
"city" : "Springfield",
"state" : {
"abbreviation" : "MA",
"name" : "Massachusetts"
},
"zip" : "01020",
"geo" : {
"lat" : 28.519,
"lng" : -84.304
}
}
},
{
"address" : {
"street" : "100 Sumner Ave",
"city" : "Springfield",
"state" : {
"abbreviation" : "IL",
"name" : "Illinois"
},
"zip" : "32822",
"geo" : {
"lat" : 22.519,
"lng" : -71.304
}
}
},
{
"address" : {
"street" : "40 Roger Ave",
"city" : "Salem",
"state" : {
"abbreviation" : "AL",
"name" : "Alabama"
},
"zip" : "32822",
"geo" : {
"lat" : 22.519,
"lng" : -71.304
}
}
}
])
The following query would give the desired result:
var res = [
{
"name": 'Massachusetts - Salem',
"city": 'Salem',
"_id": "53784206cd73fbae193b62d5",
"state": [{"name": "Massachusetts", "abbreviation": "MA"}]
}, {
"name": 'Illinois - Springfield',
"city": 'Springfield',
"_id": "5376fa92bde0e0ea047e9abd",
"state": [{"name": "Illinois", "abbreviation": "IL"}]
}
];
var condition = res.map(function (item){
return {
"address.city": item.city,
"address.state.name": item.state[0].name
}
});
db.properties.find({"$or": condition });
Sample Output
/* 0 */
{
"_id" : ObjectId("557848b43cab061ff5c618b7"),
"address" : {
"street" : "125 Oak Drive",
"city" : "Salem",
"state" : {
"abbreviation" : "MA",
"name" : "Massachusetts"
},
"zip" : "02108",
"geo" : {
"lat" : 24.519,
"lng" : -83.304
}
}
}
/* 1 */
{
"_id" : ObjectId("557848b43cab061ff5c618b9"),
"address" : {
"street" : "100 Sumner Ave",
"city" : "Springfield",
"state" : {
"abbreviation" : "IL",
"name" : "Illinois"
},
"zip" : "32822",
"geo" : {
"lat" : 22.519,
"lng" : -71.304
}
}
}