I have a requirement to implement client side field level encryption, and I implemented it using the Automatic Encryption provided by MongoDB, now I have to update the schema of an existing collection and need to encrypt the existing data in the field, for that I need to find weather the field is encrypted or not, how can I achieve it.
This is my schema
{
$jsonSchema: {
bsonType: 'object',
properties: {
title: {
bsonType: 'string'
},
author: {
bsonType: 'string'
},
category: {
bsonType: 'string'
},
name: {
encrypt: {
bsonType: 'string',
keyId: [
{
$binary: {
base64: 'pDxBjDM2RcKbaDugcRHvPg==',
subType: '04'
}
}
],
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
}
}
}
}
}
I am unable to find any lead, as I am a beginner to MongoDB.
Related
I know that this can be a repeat question but none of the existing answers solved my problem.
I have a friendsSchema like below:
const friendsSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
},
friends: [{
friendId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
},
isFromFacebook: {
type: Boolean,
default: false
},
thisFriendReceivedGiftTimeStamp: {
type: Date,
default: null
},
thisFriendSentGiftTimeStamp: {
type: Date,
default: null
}
}]
}, {
timestamps: true
});
The client is sending me an array of friendsIds.
So, I want to find all objects in friends array which matches all friendsIds and update their thisFriendReceivedGiftTimeStamp to Date.now().
Consider the client is sending 100s of ids in friendsIds array, what will be the most efficient way to achieve the result.
Thanks in advance.
db.collection.update({IF YOU WANT TO ADD QUERY ACCORDING TO OWNER FIELD PLEASE ADD HERE},
{
"$set": {
"friends.$[x].thisFriendReceivedGiftTimeStamp": new Date()
}
},
{
"arrayFilters": [
{
"x.friendId": {
$in: [
1,
3,
5
]
}
}
],
"multi": true
})
arrayFilters should work great here. But I am not 100% sure about efficiency
Here is a working mongoplayground link
My problem is reading properties of nested object, which is inside other nested object.
GraphQL
type Mapping {
id: ID!
partnerSegmentId: ID!
ctSegmentId: CtSegment!
}
type PartnerSegment {
id: ID!
name: String!
platformId: Int!
partner: Partner!
}
type Partner {
id: ID!
name: String!
}
Once I try to query it like:
{
allMappings {
partnerSegmentId {
id
name
partner {
id
}
}
}
}
I recieve:
{
"data": {
"allMappings": [
null
]
},
"errors": [
{
"message": "Cannot return null for non-nullable field Partner.name.",
"locations": [
{
"line": 8,
"column": 9
}
],
"path": [
"allMappings",
0,
"partnerSegmentId",
"partner",
"name"
]
}
]
}
Mapping schema
const mappingSchema = new mongoose.Schema(
{
partnerSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'PartnerSegment',
required: [true, 'Mapping must have partner segment id.']
},
ctSegmentId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'CtSegment',
required: [true, 'Mapping must have CT segment id.']
}
},
{ timestamps: true }
);
I tried to read separately Partner, PartnerSegment and Mapping models. All works fine. Any idea where i should search source of the problem? I've checked mongodb docs and ids looks okay. I suppose it's fault of my model.
If you would like to take a closer look it's project repo.
SOLUTION:
Garbage Id in the return value was caused by not working populate in the nested entity. The way I managed to solve the problem:
const allMappings = () =>
Mapping.find({})
.populate('user')
.populate('ctSegment')
.populate({
path: 'partnerSegment',
populate: {
path: 'partner'
}
})
.exec();
I have a simple user model defined in sailsj as shown below. The only thing of note is that a User has a reference to a User as their manager. What I am attempting to understand, is how could I query this model such that it will return users, and if any of those users are managers, then also include those sub-users, so on and so forth.
In oracle there was a "connect by prior" condition, however, I have not been successful in finding something for sails (or waterline).
module.exports = {
attributes: {
username: {
type: 'string',
unique: 'true',
required: 'true'
},
systemRoleCd: {
type: 'string',
enum: ['normal', 'admin']
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
//The users direct manager
manager: {
model: 'User'
},
email: {
type: 'string',
email: 'true',
unique: 'true'
},
//This is the encrypted password
password: {
type: 'string'
},
deleted: {
type: 'boolean'
}
}
};
With assumption: a user may have at most one manager, and may have many subordinates.
Try this in models/User.js:
subordinates: {
collection: 'user',
via: 'manager'
},
manager: {
model: 'user'
}
With .populate(), user's manager and subordinates should be returned.
Hello community :) I implemented lots of good working functionality with firebase -> realm. Now i tried to edit a structure and I am running through the wildest error messages.
What is right for sure:
Firebase sends the data
Data is Converted (e.g. Firebase has "brands" as array -> is converted to a string for Realm Schema)
The error appears when firebase updates
Not every firebase content has all fields (e.g. Like you can see out of Realm Schema some fields are optional: true)
Fields where i maybe expect an issue:
Maybe its not possible to say that the ReferentList is optional (or i implemented it wrong): See Realm Schema const ReferentsList
What i tried
Debug before realm.create (Realm set) Result: Every data came in the right format
Checked all input values if they are int, string, ...
Hopefully someone can help me here because i got completely stuck with this issue and its necesarry to continue for my project. I want to know:
The solution why or what to do
A posibility to debug realm in a better way
Thank you in advance for your time and help :)
Error message: Value not convertible to a number
Firebase datastructure
"begin" : "2017-05-15T15:50:00.000Z",
"description" : "abc",
"end" : "2017-05-15T16:15:00.000Z",
"id" : 6,
"language" : [ 1 ],
"location" : "L 1.02",
"member" : 20,
"referent" : [ 1, 3 ],
"register" : true,
"title" : "Sound of Silence",
"track" : 6,
"type" : 3,
"brands" : [ 1, 2, 3 ]
Realm Schema
const ReferentListSchema = {
name: 'ReferentList',
properties: {
id: {
type: 'int',
optional: true
}
}
}
const LanguageListSchema = {
name: 'LanguageList',
properties: {
id: 'int'
}
}
const EventSchema = {
name: 'Events',
primaryKey: 'id',
properties: {
id: 'int',
begin: {
type: 'date',
optional: true
},
end: {
type: 'date',
optional: true
},
title: 'string',
description: 'string',
register: 'bool',
member: {
type: 'int',
optional: true
},
language: {
type: 'list',
objectType: 'LanguageList'
},
location: 'string',
referent: {
type: 'list',
objectType: 'ReferentList'
},
type: 'int',
track: {
type: 'int',
optional: true
},
img: {
type: 'string',
optional: true
},
brands:{
type: 'string',
optional: true
}
}
}
Realm set
set(obj) {
realm.write(() => {
if(obj.referent){
obj.referent = obj.referent.map(function(id) {
return {id};
})
}
if (obj.language){
obj.language = obj.language.map(function(id) {
return {id};
})
}
realm.create('Events', obj, true);
});
}
Solved:!
The issue got solved through wrong data at firebase. Some Date Objects hasent been set correct.
How i got to the solution
When i tried to debugg the code i made a try/catch block around:
try{
realm.create('Events', obj, true);
}catch(error){
console.log(obj);
console.log(error);
}
Through this debug i found the right data wich was wrong. Before it just showed me all objects and afterwards the error.
I wont close this question because of the chance to help someone with the same issues.-
Background information: I'm using node.js and this is a server side script.
That being said, here's the problem. I have a JSON Object result, when using eyes.inspect(), looks like
{
user: [
{
foods: {
food: [
{
#: 'McDonalds',
#: { type: 'string' }
},
{
#: 'Seafood Topped Salmon',
#: { type: 'string' }
}
]
},
email: '****#******.edu',
name: 'Leo'
},
{
email: '****#******.edu',
food-list: {
food: [
{
#: 'KFC',
#: { type: 'string' }
},
{
#: 'KGC',
#: { type: 'string' }
}
]
},
name: 'Eric'
}
]
}
When calling console.log(result.user[0].foods.food[1]), the output is { '#': 'Seafood Topped Salmon', '#': { type: 'string' } }
So is there a way to get and set the text content of a node just like Seafood Topped Salmon and get rid of the type attribute?
Some more information: that JSON is actually parsed from an XML document. In the original xml file, the food node looks like <food type="string">McDonalds</food>. I must keep the type="string" attribute and after I'm done with editing the JSON object, I will parse it back to xml.
You can directly access the text of that embedded object to get and set it as:
result.user[0].foods.food[1]['#']