I use this command to export data from db, but it not a valid json, it doesn't have comma at the end of item, how to fix this issue?
https://docs.mongodb.com/database-tools/mongoexport/#syntax
{
"id" : "1",
"name" : "a"
}
{
"id" : "2",
"name" : "b"
}
{
"id": "3",
"name": "c"
}
{
"id": "4",
"name": "d"
}
Use the --jsonArray option (see https://docs.mongodb.com/database-tools/mongoexport/#std-option-mongoexport.--jsonArray )
mongoexport --quiet -d test -c test --pretty --jsonArray
[{
"_id": {
"$oid": "611aca090848cb8cab2943f7"
},
"id": "1",
"name": "a"
},
{
"_id": {
"$oid": "611aca110848cb8cab2943f8"
},
"id": "2",
"name": "b"
},
{
"_id": {
"$oid": "611aca180848cb8cab2943f9"
},
"id": "3",
"name": "c"
},
{
"_id": {
"$oid": "611aca1d0848cb8cab2943fa"
},
"id": "4",
"name": "d"
}]
Related
I have collection:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
{
"_id": "6",
"userId": "7",
"userName": "D",
}
I need to make some sort of request, something like so:
db.users.find([
{
"userId": "2",
"userName": "A",
},
{
"userId": "3",
"userName": "B",
},
{
"userId": "5",
"userName": "C",
}])
And I want do get:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
Sorry for that find request not so readable here. Stackoverflow thinks its so many code here and few words.
You can try something like this
db.collection.find({
$or: [
{ userId: "2", userName: "A" },
{ userId: "3", userName: "B" }
]
})
Working Mongo playground
I can't get collection.distinct to work for me on db.version '4.4.3'. I'm following the example at https://docs.mongodb.com/manual/reference/command/distinct/
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
Then run
db.runCommand ( { distinct: "inventory", key: "dept" } )
I get
{ values: [], ok: 1 }
This doesn't work either:
db.inventory.distinct( "dept" )
[]
I can't get it to work on a local server or a remote server. What gives?
For the below document, I want to write mongodb query to get the result.
[{
"id": "1",
"class": "class1",
"value": "xyz"
}, {
"id": "2",
"class": "class2",
"value": "abc"
}, {
"id": "3",
"class": "class3",
"value": "123"
}, {
"id": "4",
"class": "class4"
}, {
"id": "5",
"class": "class5",
"value": ""
}
]
The search parameter is an array of values - ["abc", "xyz", ""] and this is
going to look attribute "value"
The output should be below and in this case, the third item in the search array "" is pointing to collection that has "id" - 4 and 5 :
[{
"id": "1",
"class": "class1",
"value": "xyz"
}, {
"id": "2",
"class": "class2",
"value": "abc"
}, {
"id": "4",
"class": "class4"
}, {
"id": "5",
"class": "class5",
"value": ""
}
]
Please assist to provide the mongodb query to get the result like this
Whenever you have blank string you can add null in array, like this,
db.collection.find({
value: {
$in: ["abc", "xyz", "", null]
}
})
I have a requirement to sort data based on a defined list, not alphabetically and not numerically.
Sorting order is in a variable "sortOrder"
%dw 2.0
output application/json
var sortOrder = [ "Brad", "Alex", "Dan", "Chad"]
var myPayload = [
{
"Name": "Dan",
"Id": "3"
},
{
"Name": "Brad",
"Id": "4"
},
{
"Name": "Alex",
"Id": "2"
}
{
"Name": "Chad",
"Id": "1"
}
{
"Name": "Dan",
"Id": "5"
}
]
---
myPayload
The output needs to be like this.
[
{
"Name": "Brad",
"Id": "4"
},
{
"Name": "Alex",
"Id": "2"
},
{
"Name": "Dan",
"Id": "3"
},
{
"Name": "Dan",
"Id": "5"
},
{
"Name": "Chad",
"Id": "1"
}
]
There are 2 entries for Dan, in this case the sorting should be based on "Id"
You can order by the index of the sortOrder elements using Name to find it.
%dw 2.0
import * from dw::core::Arrays
output application/json
var sortOrder = [ "Brad", "Alex", "Dan", "Chad"]
var myPayload = [
{
"Name": "Dan",
"Id": "3"
},
{
"Name": "Brad",
"Id": "4"
},
{
"Name": "Alex",
"Id": "2"
},
{
"Name": "Chad",
"Id": "1"
},
{
"Name": "Dan",
"Id": "5"
}
]
---
myPayload orderBy( indexOf(sortOrder, $.Name) )
here are 2 possibilities for a note taking database that will have multiple notes for multiple users to keep track of
1.
[
{
"_id": "abcd",
"userInfo": {
"userID": "1",
"notes": [
{
"noteID": "1",
"text": "123"
},
{
"noteID": "2",
"text": "456"
},
{
"noteID": "3",
"text": "789"
}
]
}
},
{
"_id": "efgh",
"userInfo": {
"userID": "2",
"notes": [
{
"noteID": "1",
"text": "123"
},
{
"noteID": "2",
"text": "456"
}
]
}
}
]
And the 2nd option:
[
{
"_id": "abcd",
"userID": "1",
"noteID": "1",
"text": "123"
},
{
"_id": "efgh",
"userID": "1",
"noteID": "2",
"text": "456"
},
{
"_id": "ijkl",
"userID": "1",
"noteID": "3",
"text": "789"
},
{
"_id": "mnop",
"userID": "2",
"noteID": "1",
"text": "123"
},
{
"_id": "wxyz",
"userID": "2",
"noteID": "2",
"text": "123"
}
]
I'd expect 1 to have a much better performance when it comes to loading notes for a single user(if the user has a ton of notes). However, 2nd option is much better when modifying and adding individual notes.