I have been testing out the recently released multi-item reference fields, and thought I would share some of my experiences with importing and exporting data to collections that use this feature. As best I can tell, there is no documentation that describes this process.
Importing Data
While the Wix Data Manager can easily import CSV files into collections that use regular reference fields (you just have to know the _id of the referenced item), you cannot use the Data Manager to import data into collections that use multi-item reference fields. You can only do this using the wixData API and JSON formatted data. I confirmed this with Wix Support today.
Here is some example code from my use case: a homeowners association, where an HOA member may own multiple parcels of land. There are two collections: Member_Parcel, which contains the parcels of land, and Members, which contains the HOA members.
export async function importJSON(json, collection) {
const complete = “Data Uploaded”;
for ( let i = 0; i < json.length; i++) {
let memberid = (json[i].parcelMemberRef2);
let parcel = await wixData.insert(collection, json[i]);
let parcelid = parcel._id;
let options = {“suppressHooks”: true }
let multiref = await wixData.insertReference(collection, “parcelMemberRef2”, parcelid, memberid, options)
}
return complete
}
The JSON input file is created from a CSV file which has the parcel data and the _id of the “parcel owning” member.
There are two steps for each item in the JSON input file: First, insert the parcel data into the Member_Parcel collection (passed to the function via “json” (data) and “collection” (collection name) parameters). Second, using the _id of the newly inserted Member_Parcel record, insert the multi-item reference to connect the parcel to the correct Member. This loop is repeated until all the parcels have been inserted and all the references have been created.
I have used async/await to make sure that the promise from the wixData.insert into the Member_Parcel collection is fulfilled before executing the wixData.insertReference. I have found this to be more reliable than using .then() – although that may be my limited Javascript knowledge.
Exporting Data
The Wix Data Manager will properly export a collection with multi-item reference fields to a CSV file, but the column in the CSV file which corresponds to the multi-item reference field will contain an array string (e.g., [“527d90a5-deb4-42d5-9b71-639633af1d2b”,“4912f4b8-99db-45fb-8b33-e769714f3036”] ) even if there is only one value in the field. This can make manipulating the CSV file more complicated.
I hope this helps others who may be interested in using multi-item reference fields.