how to sort array of objects in mongodb [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How to sort sub-documents in the array field?
3 answers
My MongoDB document structure:
_id:ObjectId("12345")
hai:Array
0:Object
designation:"software"
1:Object
designation:"hardware"
2:Object
designation:"Core"
I need to sort the designation in hai array.
I tried like this, but not working
db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)
Can anyone please help,
Thanks in advance
node.js mongodb
marked as duplicate by chridam
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 8:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to sort sub-documents in the array field?
3 answers
My MongoDB document structure:
_id:ObjectId("12345")
hai:Array
0:Object
designation:"software"
1:Object
designation:"hardware"
2:Object
designation:"Core"
I need to sort the designation in hai array.
I tried like this, but not working
db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)
Can anyone please help,
Thanks in advance
node.js mongodb
marked as duplicate by chridam
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 8:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to sort sub-documents in the array field?
3 answers
My MongoDB document structure:
_id:ObjectId("12345")
hai:Array
0:Object
designation:"software"
1:Object
designation:"hardware"
2:Object
designation:"Core"
I need to sort the designation in hai array.
I tried like this, but not working
db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)
Can anyone please help,
Thanks in advance
node.js mongodb
This question already has an answer here:
How to sort sub-documents in the array field?
3 answers
My MongoDB document structure:
_id:ObjectId("12345")
hai:Array
0:Object
designation:"software"
1:Object
designation:"hardware"
2:Object
designation:"Core"
I need to sort the designation in hai array.
I tried like this, but not working
db.collection.find({_id:ObjectId("12345")}).sort(hai:-1)
db.collection.find({_id:ObjectId("12345")}).sort(hai.designation:-1)
Can anyone please help,
Thanks in advance
This question already has an answer here:
How to sort sub-documents in the array field?
3 answers
node.js mongodb
node.js mongodb
edited Jan 4 at 8:58
GeekSambhu
691819
691819
asked Jan 4 at 7:42
PRUDHVI RAJPRUDHVI RAJ
248
248
marked as duplicate by chridam
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 8:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by chridam
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 8:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You have to unwind
array first then you can able to sort data.
ref link : Sort array
add a comment |
The sort function expects an object like this (note the brackets in sort):
db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})
or
db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})
Also note if your sort is on a nested object then you need to use quotes around your key.
Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to unwind
array first then you can able to sort data.
ref link : Sort array
add a comment |
You have to unwind
array first then you can able to sort data.
ref link : Sort array
add a comment |
You have to unwind
array first then you can able to sort data.
ref link : Sort array
You have to unwind
array first then you can able to sort data.
ref link : Sort array
answered Jan 4 at 7:48
Sachin ShahSachin Shah
1,9241519
1,9241519
add a comment |
add a comment |
The sort function expects an object like this (note the brackets in sort):
db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})
or
db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})
Also note if your sort is on a nested object then you need to use quotes around your key.
Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
add a comment |
The sort function expects an object like this (note the brackets in sort):
db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})
or
db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})
Also note if your sort is on a nested object then you need to use quotes around your key.
Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
add a comment |
The sort function expects an object like this (note the brackets in sort):
db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})
or
db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})
Also note if your sort is on a nested object then you need to use quotes around your key.
Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.
The sort function expects an object like this (note the brackets in sort):
db.collection.find({_id:ObjectId("12345")}).sort({hai:-1})
or
db.collection.find({_id:ObjectId("12345")}).sort({"hai.designation":-1})
Also note if your sort is on a nested object then you need to use quotes around your key.
Aside: If you dont use the pipeline i.e. complex queries then you don't need to use unwind.
answered Jan 4 at 8:00
LongHikeLongHike
8121224
8121224
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
add a comment |
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
I think the requirement is not sorting the documents, but sorting a field inside document.
– Sreeragh A R
Jan 4 at 8:10
add a comment |