Adding document under nested documents - node.js

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?

Related

Finding an object with multiple items in mongoose

I am having certain issue with querying mongodb using mongoose, but unable to find my required output. Below is the data I am looking for the solution.
I am trying to find with two params here one is location and second is type
{
"_id" : ObjectId("5f04dcf8e123292518a863ae"),
"location" : "Australia",
"name" : "June Bill",
"filters" : [
{
"_id" : ObjectId("5f047efe9fc7ad000f44f990"),
"name" : "Q1",
"type" : "Platinum",
"conditions" : [
{
"And" : [
{
"_id" : "objectid",
"field" : "location",
"value" : "Australia",
"operator" : "equal"
},
{
"_id" : "objectid",
"field" : "name",
"value" : "admin",
"operator" : "equal"
}
]
}
]
},
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc6e"),
"name" : "Q2",
"type" : "Gold",
"conditions" : [
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc6f"),
"field" : "ocation",
"value" : "Australia",
"operator" : "equal"
},
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc70"),
"field" : "start_date",
"value" : "2020-06-01T00 3A00",
"operator" : "equal"
},
]
}
],
"__v" : 0
},
{
"_id" : ObjectId("5f04dcf8e123292518a863ae"),
"location" : "Brazil",
"name" : "June Bill",
"filters" : [
{
"_id" : ObjectId("5f047efe9fc7ad000f44f990"),
"name" : "Q1",
"type" : "Silver",
"conditions" : [
{
"And" : [
{
"_id" : "objectid",
"field" : "location",
"value" : "Australia",
"operator" : "equal"
},
{
"_id" : "objectid",
"field" : "name",
"value" : "admin",
"operator" : "equal"
}
]
}
]
},
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc6e"),
"name" : "Q2",
"type" : "Gold",
"conditions" : [
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc6f"),
"field" : "location",
"value" : "Australia",
"operator" : "equal"
},
{
"_id" : ObjectId("5f04d51c0ce40120bbb7dc70"),
"field" : "start_date",
"value" : "2020-06-01T00 3A00",
"operator" : "equal"
},
]
}
],
"__v" : 0
}
here I am trying to find with location and in filters with its type
How Can i do this in mongoose?
I tried
Model.find({'location': 'Brazil', 'filters':{ "$in" : ["Silver"]} });
But it didn't work, Can any one help me achieving actual result?
You can use the . to query the embedded filed or if you want to match multiple fields you can use $elemMatch
db.collection.find({
"location": "Brazil",
"filters.type": {
"$in": [
"Silver"
]
}
},
{
"filters.$": 1
})
MongoDB Playground
If you want to filter out the matched result you can use $ operator in projection
Use this way:
db.collection.find({
"location": "Brazil",
"filters.type": "Silver"
})
MongoDb Playground

How to update my address throug _id in myAddresses mongodb nodejs?

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
}
})

how to write a mongodb aggregation query for following description

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"
}

email id search is not working in mongodb full text search

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()

Mongo/Mongoose - Find by partial document

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
}
}
}

Resources