How to sort orders by timestamps? - node.js

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.

Related

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

Getting list of connected nodes for a given node in arangodb

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

Mongodb Aggregation and Group By query

I have set of baskets stored in mongodb like:
/* 47 */
{
"_id" : ObjectId("535ff14c2e441acf44708ec7"),
"tip" : "0",
"basketTransactionCash" : "2204",
"basketTransactionCard" : "0",
"completed" : ISODate("2014-04-07T14:35:17.000Z"),
"consumerId" : null,
"storeId" : 1,
"basketId" : 210048,
"basketProduct" : [
{
"_id" : ObjectId("535feffa2e441acf446facb7"),
"name" : "Vanilla Spice Hot Chocolate",
"productId" : 23,
"basketProductInstanceId" : 838392,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "633",
"vatPercentage" : "0.2"
}
],
"created" : ISODate("2014-04-07T14:35:42.000Z"),
"__v" : 0
}
/* 48 */
{
"_id" : ObjectId("535ff14c2e441acf44708ede"),
"tip" : "0",
"basketTransactionCash" : "230",
"basketTransactionCard" : "0",
"completed" : ISODate("2014-04-07T14:41:51.000Z"),
"consumerId" : 1,
"storeId" : 1,
"basketId" : 210072,
"basketProduct" : [
{
"_id" : ObjectId("535feffa2e441acf446facf3"),
"name" : "Melon",
"productId" : 4544,
"basketProductInstanceId" : 838430,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "200",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffa2e441acf446facf4"),
"name" : "30p",
"productId" : 8496,
"basketProductInstanceId" : 838431,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "30",
"vatPercentage" : "0"
}
],
"created" : ISODate("2014-04-07T14:42:16.000Z"),
"__v" : 0
}
/* 49 */
{
"_id" : ObjectId("535ff14c2e441acf44708ee2"),
"tip" : "0",
"basketTransactionCash" : "2204",
"basketTransactionCard" : "0",
"completed" : ISODate("2014-04-07T14:41:54.000Z"),
"consumerId" : 2,
"storeId" : 1,
"basketId" : 210076,
"basketProduct" : [
{
"_id" : ObjectId("535feffb2e441acf446fad02"),
"name" : "Creamy Natural Yoghurt",
"productId" : 69,
"basketProductInstanceId" : 839800,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "911.7",
"vatPercentage" : "0.2"
},
{
"_id" : ObjectId("535feffb2e441acf446fad03"),
"name" : "Melon",
"productId" : 4544,
"basketProductInstanceId" : 839801,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "200",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffb2e441acf446fad04"),
"name" : "30p",
"productId" : 8496,
"basketProductInstanceId" : 839802,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "30",
"vatPercentage" : "0"
}
],
"created" : ISODate("2014-04-07T14:43:00.000Z"),
"__v" : 0
}
Could I have this grouped by completed by hourly? So the end result should be something similar to the below
{
"totalTip" : "0",
"basketTransactionCashTotal" : "4638",
"basketTransactionCardTotal" : "0",
"completed" : "2014-04-07 14",
"consumerIds" : [null, 1, 2],
"storeId" : 1,
"basketIds" : [210048, 210072, 210076]
"basketProduct" : [
{
"_id" : ObjectId("535feffa2e441acf446facf3"),
"name" : "Melon",
"productId" : 4544,
"basketProductInstanceId" : 838430,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "200",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffa2e441acf446facf4"),
"name" : "30p",
"productId" : 8496,
"basketProductInstanceId" : 838431,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "30",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffb2e441acf446fad02"),
"name" : "Creamy Natural Yoghurt",
"productId" : 69,
"basketProductInstanceId" : 839800,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "911.7",
"vatPercentage" : "0.2"
},
{
"_id" : ObjectId("535feffb2e441acf446fad03"),
"name" : "Melon",
"productId" : 4544,
"basketProductInstanceId" : 839801,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "200",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffb2e441acf446fad04"),
"name" : "30p",
"productId" : 8496,
"basketProductInstanceId" : 839802,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "30",
"vatPercentage" : "0"
},
{
"_id" : ObjectId("535feffa2e441acf446facb7"),
"name" : "Vanilla Spice Hot Chocolate",
"productId" : 23,
"basketProductInstanceId" : 838392,
"quantity" : 1,
"productModifiedPrice" : null,
"productInstancePrice" : "633",
"vatPercentage" : "0.2"
}
]
}
Could anyone help me understand this for mongoose in nodejs please?
First of all, the fields tip, basketTransactionCash and basketTransactionCard need to be converted to numeric type for you to perform any arithmetic operations. You can look at this question for an approach to update all documents with the right data type.
Once the data type is taken care of, you can use the aggregation framework to get close to what you want. The date aggregation operators provide a way to group by the hour. Sample query is below:
db.collection.aggregate([
{
"$group": {
"_id": {
"year": {"$year": "$completed"},
"month": {"$month": "$completed"},
"day": {"$dayOfMonth": "$completed"},
"hour": {"$hour": "$completed"}
},
"totalTip": {
"$sum": "$tip"
},
"basketTransactionCashTotal": {
"$sum": "$basketTransactionCash"
},
"basketTransactionCardTotal": {
"$sum": "$basketTransactionCard"
},
"consumerIds": {
"$push": "$consumerId"
},
"storeId": {
"$addToSet": "$storeId"
},
"basketIds": {
"$push": "$basketId"
},
"basketProducts": {
"$push": "$basketProduct"
}
}
}
])
I would suggest you to read up on aggregation and play around with it a little bit as it's a quite powerful tool.
NOTE: Unless the data types are updated, you'll get 0 for totalTip, basketTransactionCashTotal and basketTransactionCardTotal

Resources