I'm using Angular Dynamic locale and Angular-Translate for internationalization and localization (i18n). And works well.
I like the idea of angular-translate that is possible to change the language without refresh the page.
Is possible to do the same with Angular Dynamic locale? If is possible, how can I get this?
All the words from angular-translate changed automatically, but not the words from angular_locale (datapicker, etc), that the users need refreshing the page.
Thanks!
Just in case you don't have absolute necessity to use Angular Dynamic locale,you can create your own LocaleFactory like this:
factory('LocaleFactory', function ( $locale, $translate) {
var locales = {
nl: {
"DATETIME_FORMATS": {
"AMPMS" : [
"AM",
"PM"
],
"DAY" : [
"zondag",
"maandag",
"dinsdag",
"woensdag",
"donderdag",
"vrijdag",
"zaterdag"
],
"MONTH" : [
"januari",
"februari",
"maart",
"april",
"mei",
"juni",
"juli",
"augustus",
"september",
"oktober",
"november",
"december"
],
"SHORTDAY" : [
"zo",
"ma",
"di",
"wo",
"do",
"vr",
"za"
],
"SHORTMONTH": [
"jan.",
"feb.",
"mrt.",
"apr.",
"mei",
"jun.",
"jul.",
"aug.",
"sep.",
"okt.",
"nov.",
"dec."
],
"fullDate" : "EEEE d MMMM y",
"longDate" : "d MMMM y",
"medium" : "d MMM y HH:mm:ss",
"mediumDate": "d MMM y",
"mediumTime": "HH:mm:ss",
"short" : "dd-MM-yyyy HH:mm",
"shortDate" : "dd-MM-yyyy",
"shortTime" : "HH:mm"
},
"NUMBER_FORMATS" : {
"CURRENCY_SYM": "\u20ac",
"DECIMAL_SEP" : ",",
"GROUP_SEP" : ".",
"PATTERNS" : [
{
"gSize" : 3,
"lgSize" : 3,
"macFrac": 0,
"maxFrac": 3,
"minFrac": 0,
"minInt" : 1,
"negPre" : "-",
"negSuf" : "",
"posPre" : "",
"posSuf" : ""
},
{
"gSize" : 3,
"lgSize" : 3,
"macFrac": 0,
"maxFrac": 2,
"minFrac": 2,
"minInt" : 1,
"negPre" : "\u00a4\u00a0",
"negSuf" : "-",
"posPre" : "\u00a4\u00a0",
"posSuf" : ""
}
]
}
}
};
return {
setLocale: function (key) {
$translate.use(key);
angular.copy(locales[key], $locale);
}
};
});
Similarly you can add other locals as well
Call setLocale to change the locale
run(function (LocaleFactory) {
LocaleFactory.setLocale('nl');
});
When ever your locale get changed you can call setLocale by providing the locale key as an argument. It will change your locale instantly
Related
let me give an example.
There is a collection vehicle_list
`[
{
"driver" : 1,
"driver_vehicles" : [
{
"name" : "Car1",
"state" : 1
"vehicledates" : []
},
{
"name" : "Car2",
"vehicledates" : [
{
"trip" : ObjectId("6374c5521e0056579430e15e"),
"trip_date" : "Nov 28, 2022"
},
{
"trip" : ObjectId("6374c3c65c106e84520e2bda"),
"trip_date" : "Nov 30, 2022"
},
{
"trip" : ObjectId("63789e73aab4b1b08dac4d23"),
"trip_date" : "Dec 1, 2022"
}
],
"state" : 1
},
{
"name" : "Car3"
"vehicledates" : [
{
"trip_id" : ObjectId("63776d5b9061c736099501ac"),
"trip_date" : "Dec 2, 2022"
}
],
"state" : 1
}
],
}
]
`
What i want is to have the Driver who does not have a vehicle that does not have a trip on Dec 1, 2022 and also state:1
So i made a query
db.vehicle_list.find({"driver_vehicles.vehicledates.trip_date": {$nin: ["Dec 1, 2022"]},"state":1})
This doesn't return document _id 1
Now i understand that car2 has Dec 1, 2022 so it should not return.
But Car1 and Car3 doesn't have Dec 1, 2022 and it also matches the condition state:1 so it should return the document.
Hope you undestand what i mean. Let me know if you have any doubts.
I tried this query
db.vehicle_list.find({"driver_vehicles.vehicledates.trip_date": {$nin: ["Dec 1, 2022"]},"state":1})
This doesn't return document _id 1
But it doesn't return Driver 1
Instead, I think the $elemMatch operator will work as what you need.
db.collection.find({
"driver_vehicles": {
$elemMatch: {
"vehicledates.trip_date": {
$nin: [
"Dec 1, 2022"
]
},
"state": 1
}
}
})
Demo # Mongo Playground
I have a huge number of records in the collection on the below structure.
Here I want to update all floor fields as an empty string "", wherever it's "n/a". It should not affect other blocks which already have value for the floor like the First, Second Floor.
Can someone help on this?
{
"id" : "181",
"EID" : "83",
"History" : [
{
"aNum" : "12324",
"dev" : [
{
"type" : "",
"room" : "Office",
"floor" : "Second Floor"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "n/a"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "First Floor"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "n/a"
},
]
}
]
}
With arrayFilters and filtered $[<identifier>] operator.
db.collection.update({},
{
$set: {
"History.$[].dev.$[dev].floor": ""
}
},
{
arrayFilters: [
{
"dev.floor": "n/a"
}
],
multi: true
})
Sample Mongo Playground
I have this code in Swift 4 and Alamofire:
Alamofire.request("http://xxxx.pl/id=1", method: .get, parameters: nil)
.responseJSON { response in
let jsonResponse = JSON(response.result.value!)
let resData = jsonResponse["ranking"].array
print("XXXX: \(jsonResponse)")
}
.responseString { response in
if let error = response.result.error {
print(error)
}
if let value = response.result.value {
print(value)
}
}
After running this code, I get a json with the following parameters:
XXXX: {
"ranking" : {
"dataWidoczneOd" : {
"second" : 0,
"year" : 2018,
"month" : 2,
"hourOfDay" : 0,
"dayOfMonth" : 1,
"minute" : 0
}
"opis" : "cx",
"id" : 50971,
"dataWidoczneDo" : {
"second" : 0,
"year" : 2018,
"month" : 2,
"hourOfDay" : 0,
"dayOfMonth" : 31,
"minute" : 0
},
"grupy" : [
{
"nazwa" : "yyy",
"kod" : "yyy",
"id" : 51032,
"idkiPlikowGrafiki" : [
"51034"
],
"gracze" : [
{
"zakonczonaGra" : false,
"imieINazwisko" : "zzzz yyyy",
"email" : "tertretera#cccc.com",
"liczbaZdobytychPunktow" : "0.0",
"czasGry" : "0 min"
}
]
},
{
"nazwa" : "ttt",
"kod" : "ttt",
"id" : 50981,
"idkiPlikowGrafiki" : [
"50983",
"50986"
],
"gracze" : [
]
}
],
"nazwa" : "grupowy",
"idkiPlikowGrafiki" : [
"50976"
],
"typ" : "GRUPA",
"dataOd" : {
"second" : 0,
"year" : 2018,
"month" : 2,
"hourOfDay" : 0,
"dayOfMonth" : 1,
"minute" : 0
}
}
}
How can I get the values from this json array and save in variables:
- gracze (all values)
- idkiPlikowGrafiki (all values)
- typ (all values)
- kod (all values)
- dataWidoczneOd (all values)
?
Please help :)
You should create a model class for this response and parse it with, for example, Codable. Here is a good example.
I am currently looking to sort the sub document, Clients, in a particular order based on an array.
The mongoDB structure is
{
"_id" : "1033",
"Name" : "Test",
"Clients" : [
{
"Id" : 1033,
"Types" : [
{
"Class" : "C",
"Service" : null
},
{
"Class" : "B",
"Service" : null
}
]
},
{
"Id" : 156136,
"Types" : [
{
"Class" : "A",
"Service" : null
},
{
"Class" : "B",
"Service" : null
},
{
"Class" : "C",
"Service" : null
},
{
"Class" : "D",
"Service" : null
}
]
}
]
}
I need the above document displayed in the order based on a array like [B, A, D, C]
So that the output would be as below:
{
"_id" : "1033",
"Name" : "Test",
"Clients" : [
{
"Id" : 1033,
"Types" : [
{
"Class" : "B",
"Service" : null
},
{
"Class" : "C",
"Service" : null
}
]
},
{
"Id" : 156136,
"Types" : [
{
"Class" : "B",
"Service" : null
},
{
"Class" : "A",
"Service" : null
},
{
"Class" : "D",
"Service" : null
},
{
"Class" : "C",
"Service" : null
}
]
}
]
}
Could you please help me on how to achieve this?
I am currently using MongoDB Driver for .Net
Custom ordering is possible via aggregation as specified here.
I've solved my problem with custom sorting , using $addFields and $indexOfArray aggregations.
let imagine that my document has two fields: BouqetId, Name
[
{ "BouqetId" : 2, "Name" : "Name2"},
{ "BouqetId" : 16, "Name" : "Name16"},
{ "BouqetId" : 25, "Name" : "Name25"},
{ "BouqetId" : 15, "Name" : "Name15"},
{ "BouqetId" : 125, "Name" : "Name125"},
{ "BouqetId" : 258, "Name" : "Name258"},
{ "BouqetId" : 127, "Name" : "Name127"}
...
]
and I want to search for Bouqet with Ids [258,15,2,16] and get them with this order.
1/ I filter my collection using $in operator in $match aggregation to get the required documents: view step one in code.
2/ I add a field using $addFields aggregation named Order and assign to it the index of the BouqetId in the searched array using $indexOfArray aggregation.
3/ finally I sort them using the newly added Order field.
4/ I get my custom-ordered result. I can remove the order field but it is ok for now.
here and exmaple in Nodejs :
var db = client.db("MyDatabase");
var collection = db.collection("Bouqets");
var pipeline = [
{
"$match": {
"BouqetId": {
"$in": [
258,
15,
2,
16
]
}
}
},
{
"$addFields": {
"Order": {
"$indexOfArray": [
[
258,
15,
2,
16
],
"$BouqetId"
]
}
}
},
{
"$sort": {
"Order": 1.0
}
}
];
var cursor = collection.aggregate(pipeline);
Here my result :
[
{ "BouqetId" : 258, "Name" : "Name258" , "Order" : 0},
{ "BouqetId" : 15, "Name" : "Name15", "Order" : 1},
{ "BouqetId" : 2, "Name" : "Name2", "Order" : 2},
{ "BouqetId" : 16, "Name" : "Name16", "Order" : 3}
]
here the same example in c# :
IMongoClient client = new MongoClient("mongodb://host:port/");
IMongoDatabase database = client.GetDatabase("MyDatabase");
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("Bouqets");
var options = new AggregateOptions()
{
AllowDiskUse = false
};
// my array
var searchArray = new int[] {258, 15, 2, 16};
PipelineDefinition<BsonDocument, BsonDocument> pipeline = new BsonDocument[]
{
new BsonDocument("$match", new BsonDocument()
.Add("BouqetId", new BsonDocument()
.Add("$in", new BsonArray(searchArray)
)
)),
new BsonDocument("$addFields", new BsonDocument()
.Add("Order", new BsonDocument()
.Add("$indexOfArray", new BsonArray()
.Add(new BsonArray(searchArray)
)
.Add("$BouqetId")
)
)),
new BsonDocument("$sort", new BsonDocument()
.Add("Order", 1.0))
};
var result = collection.Aggregate(pipeline, options).ToList();
Custom ordering is not possible in MongoDB. You can either sort ascending or descending.
I am working on project on which I had to make recommendation system for users in website.I am new to Mongodb.
I want to retrieve the names/id of users who have "frnds.type"=1 in below code.
{
"_id" : ObjectId("56a9fcc15b4e12369150d6ef"),
"name" : "Udit",
"venue" : {
"state" : "Rajasthan",
"city" : "Jaipur",
"ll" : [
"26.9000",
"75.8000"
]
},
"lsv" : [
0.14,
0.18,
0.24,
0.17,
0.05,
0.17,
0.05
],
"username" : "udit",
"frnds" : [
{
"id" : "amit",
"type" : 1
},
{
"id" : "nakul",
"type" : 0
},
{
"id" : "verma",
"type" : 1
}
]
}
I have written one query but it is giving wrong results
db.users.find({"username":"udit"},{"frnds":{"$elemMatch":{"type":1}}}).pretty()
I want result in this manner :
[
{
"id":"amit",
"type":1
},
{
"id":"verma",
"type":1
}
]
Try with Aggregation Framework as below.
db.users.aggregate([{
$match: {username: 'udit'},
{$unwind: '$frnds'},
{$match: {'frnds.type': 1}},
{$group: {frnds: {$push: "$frnds"}}
}]);